无法从 dll 调用函数

cannot call function from a dll

问题是我的编译器无法解析 dll 文件中的函数

这是我的图书馆代码

#ifndef DLL_SAMPLE
#define DLL_SAMPLE

#include <iostream>

class A
{
public:
    static void a();
};

#endif
#include "DllSample.h"

void A::a()
{
    std::cout << "hello, world" << std::endl; 
}

我的源代码

#include "DllSample.h"

int main(int argc, char* argv[])
{
    A::a();
    return 0;
}

我这样配置

如果我将函数内嵌在头文件中,它会起作用,但是当我在上面这样做时,将无法构建。

消息是:

1>    main.obj : error LNK2019: unresolved external symbol "public: static void __cdecl A::a(void)" (?a@A@@SAXXZ) referenced in function _main
1>    D:\Home\Document\Visual Studio 2019 Projects\ErrorShot\Debug\CallDllFunctionSample.exe : fatal error LNK1120: 1 unresolved externals
1>    The command exited with code 1120.
1>  Done executing task "Link" -- FAILED.
1>Done building target "Link" in project "CallDllFunctionSample.vcxproj" -- FAILED.
1>
1>Done building project "CallDllFunctionSample.vcxproj" -- FAILED.
1>
1>Build FAILED.
1>
1>main.obj : error LNK2019: unresolved external symbol "public: static void __cdecl A::a(void)" (?a@A@@SAXXZ) referenced in function _main
1>D:\Home\Document\Visual Studio 2019 Projects\ErrorShot\Debug\CallDllFunctionSample.exe : fatal error LNK1120: 1 unresolved externals
1>    0 Warning(s)
1>    2 Error(s)

您没有将方法(或 class)标记为 dllexport/dllimport。在您的 DLL 项目设置中,确保已定义 COMPILING_MY_DLL。假设 运行 应用程序时 DLL 的路径是正确的,一切都应该正常工作。

#ifndef DLL_SAMPLE
#define DLL_SAMPLE

#ifdef COMPILING_MY_DLL
# define MY_DLL_EXPORT __declspec(dllexport)
#else
# define MY_DLL_EXPORT __declspec(dllimport)
#endif

#include <iostream>

class A
{
public:
    MY_DLL_EXPORT static void a();
};

#endif