第一部分:原文地址:点击打开链接
error LNK2019: unresolved external symbol
(2007-05-28 10:23:43)
错误是解决了一个又一个,记下来权当是经验了。
共有11个类似的错误,我列出1个来。
模板:
template<class T>
void Blur(T* SourceImage, const int Width, const int Height);
template<class T>
void Blur(T* SourceImage, const int Width, const int Height);
template void
Blur<unsigned char>
(unsigned char*, const int, const int);
Blur<unsigned char>
(unsigned char*, const int, const int);
但链接时出错 :
Linking...
Image_demoDoc.obj : error LNK2019: unresolved external symbol "void __cdecl Blur<unsigned char>(unsigned char *,int,int)" (??$Blur@E@@YAXPAEHH@Z) referenced in function "void __cdecl OnBlur(struct HWND__ *)" (?OnBlur@@YAXPAUHWND__@@@Z)
Release/Image_demo.exe : fatal error LNK1120 : 11 unresolved externals
Linking...
Image_demoDoc.obj : error LNK2019: unresolved external symbol "void __cdecl Blur<unsigned char>(unsigned char *,int,int)" (??$Blur@E@@YAXPAEHH@Z) referenced in function "void __cdecl OnBlur(struct HWND__ *)" (?OnBlur@@YAXPAUHWND__@@@Z)
Release/Image_demo.exe : fatal error LNK1120 : 11 unresolved externals
折腾了4个小时,终于搞出来了。当然类似的错误百度搜索一大堆。我也查出原因了:
模板声明和实现要放在同一文件夹中 ,我原先把声明放在了.h文件中,把实现放在了.cpp文件中。改了后运行还是出错。真是奇怪!我还以为是什么大的错误。后来重新启动了下VS2005就好了。原来我修改后,编译环境还以以前就的中间文件来编译,怪不得改了后还是没用。现在发现,有时候这VC环境还是有点“反应慢”。
其实,LNK2019错误一般都是 compile是能找到相应的header (.h)文件,但链接时找不到相应的 lib 库文件(也含dll文件)。如 昨天调试时再linux下 pointcloudviewerJ 项目能compile 和link 正确,但在 windows下设置了项目 属性 后 能compile 但link 时 报错如下:
1>Linking...
1>LINK : D:/Experiment_Data/Microsoft VC9/PointCloudViewerJ/Debug/PointCloudViewerJ.exe not found or not built by the last incremental link; performing full link
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall osgGA::StateSetManipulator::StateSetManipulator(class osg::StateSet *)" (__imp_??0StateSetManipulator@osgGA@@QAE@PAVStateSet@osg@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall osgGA::TrackballManipulator::TrackballManipulator(void)" (__imp_??0TrackballManipulator@osgGA@@QAE@XZ) referenced in function _main
1>LINK : D:/Experiment_Data/Microsoft VC9/PointCloudViewerJ/Debug/PointCloudViewerJ.exe not found or not built by the last incremental link; performing full link
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall osgGA::StateSetManipulator::StateSetManipulator(class osg::StateSet *)" (__imp_??0StateSetManipulator@osgGA@@QAE@PAVStateSet@osg@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall osgGA::TrackballManipulator::TrackballManipulator(void)" (__imp_??0TrackballManipulator@osgGA@@QAE@XZ) referenced in function _main
。。。。。。。。
D:/Experiment_Data/Microsoft VC9/PointCloudViewerJ/Debug/PointCloudViewerJ.exe : fatal
error LNK1120: 11 unresolved externals
这就是说一个link失败,使由于osgGA::StateSetManipulator::............的unresolved external symbol,
也就是说 一定是osgGA库出了问题 。check 项目属性,发现 Additional Dependencies 中没有osgGA.lib , 所以 加上 osgGAd.lib(因为用的是Debug版)。rebuild 项目。compile 和link 通过。
另:有时候前面函数的声明 与 后面的 函数的定义 时的参数类型不同时,compile能通过,但link会出现LNK2019错误!
第二部分:原文地址:http://www.cppblog.com/Dutyboy/archive/2010/11/19/134082.html
最近写的程序一个solution里有两个projects,一个c++的project要调用c project,编译的时候一直出现link error,Error LNK2019: unresolved external symbol。查了好长时间才发现下面的解决方法,很不错。
http://blogs.msdn.com/vsdteam/archive/2005/11/20/495123.aspx
Error LNK2019: unresolved external symbol int __cdecl CeMountDBVolEx, void * __cdecl CeOpenDatabaseInSession
You have created a C++ device project and using the EDB methods from "coredll.lib". You have included the "windbase_edb.h" file in your projects source files. When you compile and link this you are getting following linker errors
error LNK2019: unresolved external symbol "int __cdecl CeMountDBVolEx(struct _CEGUID *,wchar_t *,struct _CEVOLUMEOPTIONS *,unsigned long)" (?CeMountDBVolEx@@YAHPAU_CEGUID@@PA_WPAU_CEVOLUMEOPTIONS@@K@Z) referenced in function "int __cdecl MountDbVol(void)" (?MountDbVol@@YAHXZ)
error LNK2019: unresolved external symbol "void * __cdecl CeOpenDatabaseInSession(void *,struct _CEGUID *,unsigned long *,wchar_t *,struct _SORTORDERSPECEX *,unsigned long,struct _CENOTIFYREQUEST *)" (?CeOpenDatabaseInSession@@YAPAXPAXPAU_CEGUID@@PAKPA_WPAU_SORTORDERSPECEX@@KPAU_CENOTIFYREQUEST@@@Z)
and you are wondering why this is happening?
The problem with your project is that you are using a "C" style exported library in your C++ projects. When the compiler generates mangled names for C++ functions, they are different from unmangled names generated by the C compiler and hence the C++ compiler will not be able to link with the methods imported from coredll.lib.
The solution to this problem is - while including the header windbase_edb.h, you can explicitly tell the compiler that all functions included from this header are "C" style functions by changing your inclusion as below.
extern "C"
{
#include
}
With this the C++ compiler when includes the declarations from the windbase_edb.h file, it does not do any name mangling for the functions declared in this file, and thus you should be able to link to EDB without any problems now.