【问题标题】:C++ Unresolved external symbol (LNK2019)C++ 未解析的外部符号 (LNK2019)
【发布时间】:2012-12-17 15:37:56
【问题描述】:

过去 2 天我一直在寻找类似问题的答案并将其实施到我的代码中,但收效甚微。我有一个外部 .dll(Windows)的 API,并且我的 .cpp 文件中包含头文件以引用 API。

但是我有这个问题,无论我做什么,我总是得到一个未解析的外部符号,它在我的 .h 文件中引用了这一行。是的,我使用谷歌并修改了我在代码中找到的答案,但没有成功。

Foo.h

Class Foo {
    public:
        static Foo* Interface_Get(char* dllfilename);

Foo.cpp

// I declare this just underneath the #include "Foo.h" header
Foo *foo = 0;

在我的 main 函数中,我将它声明为 this(以及其他一些很好的函数)。

//This has already been created and both Header and .dll are in the same directory.
Foo::Interface_Get("bar.dll"); 

我得到了这个错误

error LNK2019: unresolved external symbol 
    "public: static class Foo * __cdecl Foo::Interface_Get(char *)"

我已经尝试了我所知道的一切(这是我第一次创建 .dll 的经验)我觉得我错过了一些非常明显的东西,但对于我的生活我看不到它。

整个 Foo.cpp

#include "Foo.h"
Foo* Foo::Interface_Get(char* dllfilename); //May not be redeclared outside class error

Foo* foo = 0;

bool Frame()
{
if (foo->Key_Down(DIK_ESCAPE))
    return false;   
return true;
}


INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
foo->Interface_Get("bar.dll");

foo->System_SetState(grSTATE_FRAME, Frame);

foo->System_SetState(grSTATE_WINDOWED, true);

foo->System_SetState(grSTATE_KEYBOARD, true);

foo->System_Initiate();

foo->System_Start();

foo->System_Shutdown();

foo->Inferface_Release();

return 0;
}

【问题讨论】:

  • 您是否为Interface_Get(char* dllfilename);提供了函数定义?
  • Foo::Interface_Get 是您的代码,还是在 DLL 中实现?如果它是你的,它是在你的程序中定义的吗?如果在 DLL 中,是否链接 DLL 并导出函数?
  • Foo *foo = 0; 这是干什么用的?
  • 为了完整起见添加它,因为它可以帮助某人。我有一个类似的问题,我知道我做了一些愚蠢的事情。原来我忘了在方法名称之前添加ClassName:: :(

标签: c++ dll linker


【解决方案1】:

This question 解释了常见问题,在您的情况下,它(可能)是以下几种情况的组合:

  • (可能)忘记实现函数
  • 忘记__declspec(dllexport)
  • 忘记链接库

【讨论】:

  • 很可能忘记了 declspec(import/export)
  • 目前正在阅读所有这些内容。但我担心你可能是对的。
【解决方案2】:

如果您还没有这样做,您需要为Interface_Get(char* dllfilename); 提供函数定义。

这只是再次重新声明函数,您需要提供如下格式的函数{}

Foo* Foo::Interface_Get(char* dllfilename); //May not be redeclared outside class error

Foo.cpp

Foo* Foo::Interface_Get(char* dllfilename)
{
  //....
  return new Foo();
}

【讨论】:

  • Angew - Foo::Interface_Get 已在 dll 中定义。我还将它链接到包含所有函数定义的标题。 Karthik - 这样我就可以执行此操作 foo->Interface_Get(...);而不是 Foo::Interface_Get(...);
  • billz - 已经在我的 .cpp 文件中尝试过。我得到“可能不会在课堂之外重新声明”。我也试过在标题中这样做,但在课堂上,仍然不起作用。
  • 看我更新的答案,你需要提供你没有的函数定义
  • billz - 我已经在一个包含的 .cpp 文件中完成了该操作,该文件是已编译 .dll 的一部分,我希望它会找到该定义,然后允许我使用我的参数定义一个新函数INT WINAPI....
  • 那么这可能是 Luchian 提到的问题。
猜你喜欢
  • 1970-01-01
  • 2015-08-26
  • 2014-11-07
  • 2013-06-01
  • 1970-01-01
  • 2018-07-04
  • 1970-01-01
  • 2022-01-06
  • 2014-09-19
相关资源
最近更新 更多