未解析的外部符号,无法弄清楚原因

Unresolved external symbol, cannot figure out why

提问人:xcdemon05 提问时间:5/13/2013 最后编辑:xcdemon05 更新时间:5/17/2013 访问量:1191

问:

我有两个文件让我感到非常悲伤:和.以下是这两个文件的常规设置:camAVTEx.hcamAVTEx.cpp


//.h////////////////////////////////////////////////

/*
#includes to some other files
*/

class camera_avtcam_ex_t : public camera_t
{
public:
    camera_avtcam_ex_t();
    virtual ~camera_avtcam_ex_t();

private:
    //some members

public:
    //some methods

};

void GlobalShutdownVimbaSystem();

//.cpp/////////////////////////////////////////////

#include "StdAfx.h"
#include "camAVTEx.h"

//some other #includes

camera_avtcam_ex_t::camera_avtcam_ex_t()
{
}

//rest of the class' functions

void GlobalShutdownVimbaSystem()
{
    //implememtation
}

然后,在另一个目录中的文件中,我 #include .h 文件的确切位置,并尝试使用该类:


//otherfile.cpp

#include "..\..\src\HardSupport\Camera.h"
//this is the base camera class (camera_t)

#include "..\..\src\HardControl\camAVTEx.h" 
//this is indeed where both the .h and .cpp files are located

void InitCam
{
    camera_t* maincam = new camera_avtcam_ex_t();
}

void OnExit()
{
    GlobalShutdownVimbaSystem();
}

编译时,出现以下错误:

8>otherfile.obj:错误 LNK2001:未解析的外部符号“public:__cdecl camera_avtcam_ex_t::camera_avtcam_ex_t(void)“ (??0camera_avtcam_ex_t@@QEAA@XZ)

8>otherfile.obj:错误 LNK2001:未解析的外部符号“void __cdecl GlobalShutdownVimbaSystem(void)“ (?GlobalShutdownVimbaSystem@@YAXXZ)

8>....\bin\x64\Release\otherfile.exe:致命错误LNK1120:2 个未解决的外部文件

我一辈子都无法弄清楚为什么它找不到这两个函数的实现。

所以我想我的问题相当明显:为什么我会遇到这些错误,我需要改变什么来修复它们?

C++ 对象 继承 链接器 unresolved-external

评论

0赞 Syntactic Fructose 5/13/2013
您确定该文件有权访问程序中的包含文件吗?您的文件可能找不到这些包含/
0赞 Offirmo 5/13/2013
相当明显,但是您确定这是与您的文件一起编译和链接的吗?你的构建系统是什么?camAVTEx.cppotherfile.cpp
0赞 xcdemon05 5/13/2013
@Need4Sleep我不明白它怎么无法访问
0赞 Roddy 5/13/2013
这是因为 CamAVTex.o 没有链接到您的可执行文件中。这与 #include 文件无关。
1赞 Roddy 5/13/2013
@xcdemon05 - 这不就是你在三月份问的问题吗???

答:

0赞 Offirmo 5/15/2013 #1

不管你怎么看,你遇到的错误:意味着编译器知道符号(即类构造函数),因为他在文件中看到了它的声明,但哈拉斯,它找不到(=解析)这个符号的实现(简而言之,代码)。unresolved external symbol "public: __cdecl camera_avtcam_ex_t::camera_avtcam_ex_t(void)" (??0camera_avtcam_ex_t@@QEAA@XZ)camera_avtcam_ex_t::camera_avtcam_ex_camAVTEx.h

这通常是由于以下几个可能的原因造成的:

  • 您没有告诉编译器您尝试使用的代码(.cpp),因此他不知道。尝试将文件添加到项目中。
  • 您编译了缺失的代码,但不与之链接。检查您是否没有两个单独的项目,或者尝试将库添加到您的项目中(如果它来自一个库)。
  • 在某种程度上,编译后的代码与其定义不匹配(在混合 C 和 C++ 或弄乱命名空间时发生) 检查您是否没有声明与封闭命名空间相矛盾。
  • (也许我不知道其他原因?

评论

0赞 xcdemon05 5/17/2013
我觉得我在这里的问题可能是你的第二个要点。我还不太擅长链接器设置,知道如何在 VS2010 中执行此操作吗?