【问题标题】:dllexport functions not exporteddllexport 函数未导出
【发布时间】: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 文件中导出,甚至文档都没有这样说。只需要在声明中

标签: c++ dllimport dllexport


【解决方案1】:

为了使在源文件中声明的函数能够真正导出 DLL,您必须执行以下两项操作之一:

或者 (1) 将 __declspec(dllexport) 属性添加到函数在定义它的源代码中(不仅仅是在标题中)。

或者 (2) 将函数添加到模块定义文件 (.def) 中的 EXPORTS 列表中。

现有文档(尤其是旧文档)倾向于假定使用 .def 文件 EXPORTS 方法,这可能会产生很大的误导。

【讨论】:

  • 好的,感谢您的回答并声明 msdn 文档不好,我将发布一个指向堆栈溢出的链接,以确认很快不需要 cpp 中的 EXPORT。
  • @metablaster MSDN 文档通常非常好,IMO。只是你需要戴着微软兼容的眼镜来阅读它! (随时间而来。)
  • 好的,没问题,我喜欢 MSDN,但也可以在这里查看:stackoverflow.com/questions/29471901/…
  • @metablaster 问题更复杂:(非)导出的类和“EXPORT”宏的定义相互矛盾。
  • 这个答案不正确,MSVC++ 对声明中的 __declspec 很满意,不需要在定义中重复。
猜你喜欢
  • 2021-09-17
  • 1970-01-01
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 2012-02-11
相关资源
最近更新 更多