错误 LNK2019:未解析的外部符号 - 问题的不可见实例

error LNK2019: unresolved external symbol - unseen instance of the issue

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

>我有一个错误LNK2019:未解决的外部符号问题。

我有 2 个文件,并将它们移动到共享位置,以便 2 个不同的项目可以使用类。那里还有其他类似的类。

问题是,当我从其中一个项目的主项目调用类时,我得到了

error LNK2019: unresolved external symbol "public: __thiscall CProcessCommandLine::~CProcessCommandLine(void)" (??1CProcessCommandLine@@QAE@XZ) referenced in function _main
error LNK2019: unresolved external symbol "public: bool __thiscall CProcessCommandLine::Wait(void)const " (?Wait@CProcessCommandLine@@QBE_NXZ) referenced in function _main
error LNK2019: unresolved external symbol "public: class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > const & __thiscall CProcessCommandLine::getTargetNamesVect(void)" (?getTargetNamesVect@CProcessCommandLine@@QAEABV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@XZ) referenced in function _main
error LNK2019: unresolved external symbol "public: __thiscall CProcessCommandLine::CProcessCommandLine(void)" (??0CProcessCommandLine@@QAE@XZ) referenced in function _main

当我打开头文件并右键单击其中一个函数并转到定义时,它不会转到.cpp中的定义。当我右键单击.cpp中的函数并说转到定义时,它说"符号未定义"。

有人知道为什么会发生这种情况吗?.h 和 .cpp 位于同一位置,并且该位置位于项目的附加包含目录中

编辑:

此文件夹中的其他文件中存在以下代码:

#ifdef PROJ1
#include "class1.h"
#elif PROJ2
#include "class2.h"
#endif

我以前从未见过这些东西。它们在代码中使用,但仅用于包含正确的标头,具体取决于它在哪个项目中使用。以上是此文件夹中其他类的.cpp。所以我把它放在这里。但在其他类中,它不属于的项目是灰色的。当我把它放到 .h 中时,它会变灰正确的标题。但由于某种原因,这个.cpp没有被认为是项目的一部分。

如果你的函数属于与你正在构建的模块不同的模块,你必须导出这些类:

#ifdef THIRD_MODULE
#define DLLIMPEXP _declspec(dllexport)
#else
#define DLLIMPEXP _declspec(dllimport)
#endif
class DLLIMPEXP CProcessCommandLine
{
   //...
};

生成定义此类的项目,并定义THIRD_MODULE

此项目将生成.lib文件,您必须将其添加到主项目的链接器设置中的其他依赖项中。