C++ 如何解决“错误 LNK2019:未解析的外部符号”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/849238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:34:38  来源:igfitidea点击:

How can I resolve "error LNK2019: unresolved external symbol"?

c++visual-c++linkerstatic-libraries

提问by Attilah

I've got this MFC application I'm working on that needs to have an embedded database. So I went hunting for a slick, fast "embeddable" database for it and stumbled accross SQLite.

我有一个我正在处理的 MFC 应用程序,它需要一个嵌入式数据库。因此,我为它寻找一个灵活、快速的“可嵌入”数据库,并偶然发现了 SQLite。

I created a DB with it, and I created a static library project with Visual Studio 2008. the library project will be used in another main project.

我用它创建了一个数据库,并用 Visual Studio 2008 创建了一个静态库项目。该库项目将在另一个主项目中使用。

In the library project, I created a class DBClasswith a method AddFeedToDB(CFeed f). The library project uses the .libfile from codeproject (cppsqlite3.lib).

在库项目中,我创建了一个DBClass带有方法的类AddFeedToDB(CFeed f)。库项目使用.lib来自 codeproject ( cppsqlite3.lib)的文件。

When compiling the static library, no error is detected, but when I try to use the library project file in the main project, I get these type of errors:

编译静态库时,没有检测到错误,但是当我尝试在主项目中使用库项目文件时,出现以下类型的错误:

error LNK2019: unresolved external symbol "public:void __thiscall
   CppSQLite3DB::close(void)" (?close@CppSQLite3DB@@QAEXXZ 
   referenced in function "public: int __thiscall
   CTalkingFeedsDB::AddFeedToDB(class CFeed,char const*)" (?
   AddFeedToDB@CTalkingFeedsDB@@QAEHVCFeed@@PDB@Z

What am I missing?

我错过了什么?

采纳答案by xtofl

It happened to me more than once that I thought symbol XXX(i.e. ?close@CppSQLite3DB@@QAEXXZ) wasin the import lib, while the actual symbol was __impXXX(i.e. __imp?close@CppSQLite3DB@@QAEXXZ).

它发生在我身上不止一次,我认为符号XXX(即?close@CppSQLite3DB@@QAEXXZ在导入库,而实际符号为__impXXX(即__imp?close@CppSQLite3DB@@QAEXXZ)。

The reason for the linker error is then to be found in the compilation step: the compiler will generate the ?close@CppSQLite3DB@@QAEXXZsymbol to be imported, where it shouldgenerate __imp?close@CppSQLite3DB@@QAEXXZ. This often means that the function declaration itself didn't have __declspec( dllimport ). Which may be caused by some preprocessor symbol not being defined. Or the __declspecnot being there at all...

然后在编译步骤中找到链接器错误的原因:编译器会生成?close@CppSQLite3DB@@QAEXXZ要导入的符号,它应该在那里生成__imp?close@CppSQLite3DB@@QAEXXZ. 这通常意味着函数声明本身没有__declspec( dllimport ). 这可能是由于未定义某些预处理器符号引起的。或者__declspec根本不存在...

回答by Luis Eduardo

I know it is already 2 years since this question... but i run in the same situation here. Added all the header files... added the lib directories.. and keep having this error. So i added manually the lib to the Configuration Properties -> Linker -> Input -> Aditional Dependencies and all works for me.

我知道这个问题已经过去 2 年了……但我在这里遇到了同样的情况。添加了所有头文件...添加了 lib 目录...并不断出现此错误。所以我手动将 lib 添加到配置属性 -> 链接器 -> 输入 -> 附加依赖项,一切都对我有用。

回答by Ago

Don't know if it is your case, but the imp prefix may mean that you are compiling a x64 library in a Win32 project.

不知道是否是您的情况,但 imp 前缀可能意味着您正在 Win32 项目中编译 x64 库。

回答by Mark Ransom

The compiler and linker will not link one library into another (unless one is a DLL). You need to specify both libraries (cppsqlite3.lib and your own static library) in your main project.

编译器和链接器不会将一个库链接到另一个库(除非一个库是 DLL)。您需要在主项目中指定两个库(cppsqlite3.lib 和您自己的静态库)。

回答by MickaelFM

You either need to link the codeproject SQLite lib to your executable, or to include the sources files in your project directly. (Which one did you do ?)

您需要将 codeproject SQLite 库链接到您的可执行文件,或者将源文件直接包含在您的项目中。(你做了哪一个?)

回答by xtofl

I would follow these steps:

我会按照以下步骤操作:

  1. think about what library or .obj file you expect the symbol to be exported by.

  2. check whether it actually doesexport that very symbol (check character-wise). Sometimes, it's the calling convention differs.

  3. check if the library you expect to contain the symbol is known to the linker - first check for the 'additional libraries', then check if the library is actually found (I mostly do this by using filemon.exe from sysinternals, and look for link.exe to open the lib file. )

  1. 考虑您希望符号导出的库或 .obj 文件。

  2. 检查它是否确实导出了那个符号(检查字符)。有时,调用约定不同。

  3. 检查链接器是否知道您希望包含符号的库 - 首先检查“附加库”,然后检查是否确实找到了该库(我主要通过使用 sysinternals 中的 filemon.exe 来执行此操作,并查找链接.exe 打开 lib 文件。)

After thinking a while, you may find that your library project will notexport the sought for function. That function is in the database lib. You should add that lib to your main project. It's no use adding it to your static lib project.

想了想,你可能会发现你的库项目不会导出所寻求的功能。该函数位于数据库库中。您应该将该库添加到您的主项目中。将它添加到您的静态库项目中是没有用的。