首页 > 解决方案 > dllexport 函数未导出

问题描述

我已经根据 MSDN 文档完成了所有工作,但我的函数仍然没有被导出。 链接1链接2 链接 3

这是我拥有的(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) 在函数 main 2>C:\Users\User\source\repos\WindowsSuperLibrary\x64\Debug DLL\MathSample.exe 中引用:致命错误 LNK1120: 5 unresolved externals 2>完成构建项目“MathSample.vcxproj”——失败。\Users\User\source\repos\WindowsSuperLibrary\x64\Debug DLL\MathSample.exe : 致命错误 LNK1120: 5 unresolved externals 2>Done building project "MathSample.vcxproj" -- 失败。\Users\User\source\repos\WindowsSuperLibrary\x64\Debug DLL\MathSample.exe : 致命错误 LNK1120: 5 unresolved externals 2>Done building project "MathSample.vcxproj" -- 失败。

你能解释一下这里有什么问题吗?

我已经对 dll 运行了 dumpbin /EXPORTS 并且可以确认没有导出任何函数。

COMPILE_DLL宏当然是在 dll 项目中定义的。

我确实将导入库包含在我的项目中。

头文件和 cpp 文件中的命名空间名称相同。

标签: c++dllimportdllexport

解决方案


为了在源文件中为 DLL 声明的函数实际被导出,您必须执行以下两项操作之一:

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

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

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


推荐阅读