我很难在Visual Studio上编译C / C ++ dll。它似乎与CRT库有关。
一个简单的VS c ++ DLL项目。刚刚删除了重新编译的头文件,并添加了/ NOENTRY。导出方法正在运行,但是一个简单的类(不打算导出)会引发很多与CRT库的链接错误:
Test.h:
class ITest
{
public:
virtual void foo() = 0;
}
class Test final : public ITest
{
public:
Test();
~Test();
void foo();
};
// extern "C" __declspec(dllexport) ITest* __cdecl CreateTest();
TEST.CPP
#include "Test.h"
Test::Test() {}
Test::~Test() {}
Test::foo() {}
// extern "C" __declspec(dllexport) ITest* __cdecl CreateTest() { return new Test; }
错误:
Test.obj : error LNK2001: unresolved external symbol _purecall
msvcrt.lib(delete_scalar.obj) : error LNK2019: unresolved external symbol free referenced in function "void __cdecl operator delete(void *)" (??3@YAXPEAX@Z)
取消注释导出会引发更多msvcrt.lib链接错误并更改MDd,MT,MTd的运行时库仅更改引用的库。
谢谢你的帮助:)
问题来自Project Properties-> Linker-> Advanced-> No Entry Point中的/ NOENTRY选项。删除它解决了这个问题
/NOENTRY
用于创建dll,它们根本没有任何代码,只能包含位图等:https://docs.microsoft.com/en-us/cpp/build/creating-a-resource-only-dll?view=vs-2017
由于dll(应该)没有代码,因此visual studio不会在运行时库中链接,但是因为有代码它会生成对未解析的运行时库符号的要求。