我在 XP SP3 上的代码块 10.05 mingw 上,我基本上在 mingw 站点创建 dll 并链接到它之后构建了 dll 和 lib 文件。我遇到的问题是我试图链接到图书馆的第二部分。我创建了一个控制台应用程序并使用了以下源:
#include <stdio.h>
#include "example_dll.h"
int main(void)
{
hello("World");
printf("%d\n", Double(333));
CppFunc();
MyClass a;
a.func();
return 0;
}
然后我设置我的链接器设置,就像网站所说的添加-L. -lexample_dll
,我还链接库文件 libexample_dll.a,就像我与许多其他已成功使用相同设置的库一样。然后,当我尝试链接可执行文件时出现错误,
C:\example_dll_client\example_dll_client.cpp|2|error: example_dll.h: No such file or directory|
C:\example_dll_client\example_dll_client.cpp||In function 'int main()':|
C:\example_dll_client\example_dll_client.cpp|6|error: 'hello' was not declared in this scope|
C:\example_dll_client\example_dll_client.cpp|7|error: 'Double' was not declared in this scope|
C:\example_dll_client\example_dll_client.cpp|8|error: 'CppFunc' was not declared in this scope|
C:\example_dll_client\example_dll_client.cpp|10|error: 'MyClass' was not declared in this scope|
C:\example_dll_client\example_dll_client.cpp|10|error: expected ';' before 'a'|
C:\example_dll_client\example_dll_client.cpp|11|error: 'a' was not declared in this scope|
||=== Build finished: 7 errors, 0 warnings ===|
我还包括了用于构建 dll 和 lib 文件的两个文件(不是项目,而是这个站点以供参考。我只有错误的源作为整个项目,否则 dll 的意义何在?) .
//example_dll.cpp
#include <stdio.h>
#include "example_dll.h"
__stdcall void hello(const char *s)
{
printf("Hello %s\n", s);
}
int Double(int x)
{
return 2 * x;
}
void CppFunc(void)
{
puts("CppFunc");
}
void MyClass::func(void)
{
puts("MyClass.func()");
}
//example_dll.h
#ifndef EXAMPLE_DLL_H
#define EXAMPLE_DLL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BUILDING_EXAMPLE_DLL
#define EXAMPLE_DLL __declspec(dllexport)
#else
#define EXAMPLE_DLL __declspec(dllimport)
#endif
void __stdcall EXAMPLE_DLL hello(const char *s);
int EXAMPLE_DLL Double(int x);
#ifdef __cplusplus
}
#endif
// NOTE: this function is not declared extern "C"
void EXAMPLE_DLL CppFunc(void);
// NOTE: this class must not be declared extern "C"
class EXAMPLE_DLL MyClass
{
public:
MyClass() {};
virtual ~MyClass() {};
void func(void);
};
#endif // EXAMPLE_DLL_H
编辑:
我对 DLL 和 lib 文件的编译进行了以下更改。
我首先确保我的构建目标是一个动态库,可以通过右键单击项目管理器树中活动项目的属性在构建目标选项卡下找到它。我还检查了创建导入库和检查 .def 导出文件。然后我进入代码块构建选项并在编译器设置->其他选项选项卡下添加
-c -共享
在我添加的#defines 选项卡下
BUILDING_EXAMPLE_DLL BUILD_DLL
我的链接器设置在链接库中有 user32,在链接器设置下我有 -mwindows --out-imlib
构建现在应该编译和链接,没有错误或警告。
为了使用 int main() 编译源代码,我包含了头文件以及这些设置:
在我有 libexample_dll.dll.a 的链接库和我有的链接器设置下
-L。-lexample_dll