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

2022-01-11 00:00:00 visual-c++ linker c++ static-libraries

我正在开发这个 MFC 应用程序,它需要一个嵌入式数据库.因此,我开始为它寻找一个灵活、快速的可嵌入"数据库,并偶然发现了 SQLite.

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.

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

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.

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

In the library project, I created a class DBClass with a method AddFeedToDB(CFeed f). The library project uses the .lib file from 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

我错过了什么?

推荐答案

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

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

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

The reason for the linker error is then to be found in the compilation step: the compiler will generate the ?close@CppSQLite3DB@@QAEXXZ symbol to be imported, where it should generate __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 __declspec not being there at all...

相关文章