c++ - 链接器错误。 MinGW 无法从 MSVS dll 导出类

标签 c++ dll linker mingw

我使用 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/

相关文章:

c++ std::map获取最高键低于X的元素?

c++ - wxWidgets - 通过 DLL 添加项目到 sizer

c++ - 从 DLL 中包含 header 的理想方式?

python加载静态库

xcode - 由于架构不匹配,将静态库从 Xcode 链接到 Lazarus 应用程序失败

c++ - 不能使用隐式链接的 .dll 中的类

android - 什么时候链接 "Sdk Assemblies Only"?

c++ - 设置或咨询 boolean 值。哪个性能最好?

c++ - 如何修复腐 eclipse 的矩形?

c++ - 获取控制台句柄