提问人:xcdemon05 提问时间:5/13/2013 最后编辑:xcdemon05 更新时间:5/17/2013 访问量:1191
未解析的外部符号,无法弄清楚原因
Unresolved external symbol, cannot figure out why
问:
我有两个文件让我感到非常悲伤:和.以下是这两个文件的常规设置:camAVTEx.h
camAVTEx.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 个未解决的外部文件
我一辈子都无法弄清楚为什么它找不到这两个函数的实现。
所以我想我的问题相当明显:为什么我会遇到这些错误,我需要改变什么来修复它们?
答:
不管你怎么看,你遇到的错误:意味着编译器知道符号(即类构造函数),因为他在文件中看到了它的声明,但哈拉斯,它找不到(=解析)这个符号的实现(简而言之,代码)。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++ 或弄乱命名空间时发生) 检查您是否没有声明与封闭命名空间相矛盾。
- (也许我不知道其他原因?
评论
camAVTEx.cpp
otherfile.cpp