我在XP SP3上的代码块10.05 mingw上,我基本上有以下the mingw site on creating a dll and linking to it内建的dll和lib文件。我遇到的问题是我试图链接到图书馆的第二部分。我创建了一个控制台应用程序和使用以下来源:建立DLL并链接到它?
#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下 -shared
和我说
BUILDING_EXAMPLE_DLL BUILD_DLL
#定义选项卡下我的连接器设置在链接库,并根据连接器设置我 有USER32 -mwindows --out-IMPLIB
构建应该现在编译并链接,没有错误或警告。
为了与INT主()编译源我包括在该头文件以及这些设置:
根据链接库我不得不libexample_dll.dll.a和下接头设置我不得不
-L。 -lexample_dll
错误的第一行是一个很好的提示。你是否指定了'example_dll.h'所在的include目录? (使用'-I'编译器开关)。 – greatwolf 2013-02-19 13:00:36
正确,但不是它应该可以编译我的int main()代码只使用lib文件吗?或者我如何使用.DLL文件编译源代码? – pandoragami 2013-02-19 14:00:24
没关系,头文件只包含函数调用等。没有别的。 cpp文件包含实际操作。 – pandoragami 2013-02-19 14:07:50