dllexport 函数未导出

met*_*ter 1 c++ dllimport 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);
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

注意: dll项目中还有其他几个头文件和cpp文件,都在编译,但没有导出任何函数。

应用程序主.cpp

#include "functions.h"

int main()
{
    wsl::PointInTriangle(0, 0, 20, 0, 10, 30, 10, 15);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

程序输出:

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 个未解析的外部 2>完成构建项目“MathSample.vcxproj”——失败。

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

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

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

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

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

Adr*_*ica 6

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

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

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

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