LNK2019 VS2012错误(未解析的外部符号)

LNK2019 VS2012 error (unresolved external symbol)

本文关键字:外部 符号 VS2012 错误 LNK2019      更新时间:2023-10-16

我做了2个项目:一个是提供者,我在里面添加了一个类DH:

using namespace std;
#ifdef PROVIDER_EXPORTS
#define DH_API __declspec(dllexport)
#else
#define DH_API __declspec(dllimport)
#endif
#include <iostream>
namespace Provider
{
    class DH
    {
    public:
        static DH_API std::string GetKey();
    };
}

和小鬼:

#include "DH.h"
using namespace std;
namespace Provider
{

    std::string DH::GetKey()
    {
        return "KEY";
    }
}

当我转储dll时,我得到:

?GetKey@DH@Provider@@SA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ = @ILT+10(?GetKey@DH@Provider@@SA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)

在一个新的项目上——测试者包括h。h我已经将Provider项目的调试文件夹添加到链接目录

然后调用DH::GetKey编译后得到:

LNK2019: unresolved external symbol "__declspec(dllimport) public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl Provider::DH::GetKey(void)" (__imp_?GetKey@DH@Provider@@SA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)

怎么了?

您需要设置项目依赖项或将dll的.lib文件添加到tester项目的lib输入中,以便它知道在哪里搜索导入的函数

您错过了导入的GetKey()函数。这里有一个Project References部分,您应该添加定义该函数的项目。