我使用 MinGw (i686-4.9.2-win32) 并尝试从 dll 中导出类(这是在 visual studio 中制作的)
但是我得到一个错误 undefined reference to _imp___ZN11ConsoleTestC1E'
但是从同一个库导出的函数 foo()
没有错误..
这是我的代码
testdll.h
#ifdef CONSOLETEST_EXPORT
#define CONSOLETEST_API __declspec(dllexport)
#else
#define CONSOLETEST_API __declspec(dllimport)
#endif
extern "C" class CONSOLETEST_API ConsoleTest{
public:
ConsoleTest();
};
extern "C" void CONSOLETEST_API foo();
testdll.cpp
#include "testdll.h"
ConsoleTest::ConsoleTest(){}
void foo(){}
在 main.cpp 中:
#include "testdll.h"
int main()
{
foo();
ConsoleTest* cc = new ConsoleTest();
}
PS:我试图从 .dll 创建一个 .a 库:
pexports testdll.dll | sed "s/^_//" > testdll.def
dlltool -U -d testdll.def -l testdll.a
...但这并没有帮助。
提前感谢您的任何建议!
最佳答案
extern "C"class
将不起作用。
您可以 dll 导出类,但不能使用 C 命名约定。
ISO C++03 7.5[dcl.link]/4:
A C language linkage is ignored for the names of class members and the member function type of class member functions.
如果您删除 extern "C"
,我认为没有理由它不起作用。
关于c++ - 链接器错误。 MinGW 无法从 MSVS dll 导出类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37088979/