【发布时间】:2020-01-22 17:35:37
【问题描述】:
我已根据 MSDN 文档完成所有操作,但我的功能仍然没有导出。 LINK1LINK2LINK3
这是我拥有的(2 个项目,DLL 和从 dll 导入的应用程序),看起来像这样(短版):
DLL 函数.h
#prgma once
#ifdef COMPILE_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif // COMPILE_DLL
namespace wsl
{
EXPORT bool PointInTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y);
}
DLL 函数.cpp
#include "functions.h"
namespace wsl
{
bool PointInTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{
// implementation...
return true;
}
}
DLL dllmain.cpp
#include <Windows.h>
BOOL APIENTRY DllMain(
[[maybe_unused]] HMODULE hModule,
DWORD ul_reason_for_call,
[[maybe_unused]] LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
注意:dll项目中还有其他几个头文件和cpp文件,都在编译中,但没有导出函数。
应用程序 main.cpp
#include "functions.h"
int main()
{
wsl::PointInTriangle(0, 0, 20, 0, 10, 30, 10, 15);
return 0;
}
程序输出:
2>main.obj:错误 LNK2019:未解析的外部符号 "__declspec(dllimport) bool __cdecl wsl::PointInCircleSector(int,int,int,float,float)" (__imp_?PointInCircleSector@wsl@@YA_NHHHMM@Z) 在函数中引用 main 2>main.obj:错误 LNK2019:未解析的外部符号 "__declspec(dllimport) bool __cdecl wsl::PointInTriangle(int,int,int,int,int,int,int,int)" (__imp_?PointInTriangle@wsl@@YA_NHHHHHHHH@Z) 在函数中引用 main 2>main.obj:错误 LNK2019:未解析的外部符号 "__declspec(dllimport) bool __cdecl wsl::PointInEllipse(int,int,int,int,int,int)" (__imp_?PointInEllipse@wsl@@YA_NHHHHHH@Z) 在函数 main 中引用 2>main.obj:错误 LNK2019:未解析的外部符号 "__declspec(dllimport) bool __cdecl wsl::PointInCircle(int,int,int,int,int)" (__imp_?PointInCircle@wsl@@YA_NHHHHH@Z) 在函数 main 中引用 2>main.obj:错误 LNK2019:未解析的外部符号 "__declspec(dllimport) bool __cdecl wsl::PointInRectangle(int,int,int,int,int,int)" (__imp_?PointInRectangle@wsl@@YA_NHHHHHH@Z) 在函数中引用 主 2>C:\Users\User\source\repos\WindowsSuperLibrary\x64\Debug DLL\MathSample.exe : 致命错误 LNK1120: 5 unresolved externals 2>完成构建项目“MathSample.vcxproj”——失败。
你能解释一下这里有什么问题吗?
我已经对 dll 运行了 dumpbin /EXPORTS 并且可以确认没有导出任何函数。
COMPILE_DLL宏当然是在dll项目中定义的。
我确实在我的项目中包含了导入库。
头文件和cpp文件中的命名空间名称相同。
【问题讨论】:
-
除了链接之外,请尝试dependencywalker 找出导出的内容,因为它可能不是导致链接问题的原因。要检查的另一件事是有效版本和无效版本之间的区别。对于一个有效的,我建议使用 VS 的自动生成的 DLL 项目框架。如果这仍然没有给您任何见解,请提取minimal reproducible example 以在此处发布。顺便说一句:因为它避免了关于您发布的代码中可能相关的拼写错误的无用讨论,因此需要代码问题。
-
我已经尝试过使用默认项目模板,并且还使用 dumpbin 检查了导出,没有任何内容被导出。但是,如果除了头文件之外,我确实将
EXPORT放入 cpp 文件中的函数定义中,是的,它将被导出,但它应该通过将宏放入声明中来工作。 (编辑:我认为这是最小的可重现示例) -
正如@AlexF 所说,您的 DLL 源 (.cpp) 文件中需要
EXPORT属性,而不仅仅是在标题中。 -
好的,我省略了命名空间,我会更新我的帖子。当然,我确实将库导入到客户端应用程序中
-
@Adrian 绝对有效,但是有很多在线和 stackoverflow 答案告诉我们不需要在 cpp 文件中导出,甚至文档都没有这样说。只需要在声明中