我曾经遇到的Link2001的错误

来源:互联网 发布:淘宝店代销怎么做 编辑:程序博客网 时间:2024/04/30 14:17

1.错误现象:

   Linking...
   LINK : warning LNK4075: ignoring /EDITANDCONTINUE due to /INCREMENTAL:NO specification
   Creating library skexcel___Win32_Release/skexcel.lib and object skexcel___Win32_Release/skexcel.exp
   SK_Excel.obj : error LNK2001: unresolved external symbol "public: void __thiscall                    EXCEL::_Worksheet::SetName(unsigned short const *)" (?SetName@_Worksheet@EXCEL@@QAEXPBG@Z)
    SK_Excel.obj : error LNK2001: unresolved external symbol "public: struct IDispatch * __thiscall      EXCEL::Sheets::Add(struct tagVARIANT const &,struct tagVARIANT const &,struct tagVARIANT const &,struct tagVARIANT   const &)" (?Add@Sheets@EXCEL@@QAEPAUID
ispatch@@ABUtagVARIANT@@000@Z)

../../../bin/Client/Release/skexcel.dll : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

skexcel.dll - 3 error(s), 1 warning(s)

 

分析:造成上述2001的错误的原因有三:

1. 头文件中声明了某个函数,但是在CPP文件中没有实现它

2. 如果某个函数是在  A.H 和 A.CPP中声明和实现,在B.CPP中调用,则是没有包含 A.H 和 A.CPP

3. 没有将对应的Lib文件添加到工程中...(Project-->Setting-->Link)(暂时不理解)

 

补充个问题:

1.在2011年03月31日,我合并代码的时候,用Merge拷贝代码从低版本到高版本中去,但却没有在工程中手动添加关联文件,也会出现

   这种错误...特此标记

权威解释:

MSDN: http://msdn.microsoft.com/en-us/library/Aa234493

 

一个类似的问题的解释:

error LNK2019: unresolved external symbol (下面的不是很理解,类似于SDK的包含等)

http://www.cplusplus.com/forum/general/12144/


Okay, I've never used whatever library it is you're using (I don't even know what CORBA is, actually), but the ordinary method for linking to a library with VC++ is this:
You should have gotten with the library at least the first two of these three:
1. One or more headers that tell the compiler what the functions look like. You obviously have this, or you'd be getting compiler errors.
2. One or more .libs that defines some or all of the functions, and tells the linker to shut up about the ones that aren't defines.
3. One or more DLLs that will be linked to the program at run time if some of the functions weren't fully defined by the .libs.

Now, assuming you have number 2, you should put it somewhere where the compiler can find them. I would set aside a directory in My Documents to put all .libs and then add it to the compiler list in tools>options>projects and solutions>VC++ directories>show directories for: library files. If you're feeling lazy, you can just leave them in one of the directories that are already listed there (e.g. "$(VSInstallDir)lib").
Next, you need to add their file names (not their full path. It will still work if you add the full path, but it's unnecessary thanks to the previous step) to that option you mentioned (Configuration Properties -> Linker -> Input -> Additional Dependencies). Don't forget the file name extensions.

That should clear the linker errors.