在 vscode for c++ 中使用 sqlite3(未定义对 'xxx' 的引用)(我使用了 -lsqlite3)

Use sqlite3 in vscode for c++(undefined reference to `xxx')(I have use -lsqlite3)

本文关键字:-lsqlite3 xxx 引用 for vscode c++ 未定义 sqlite3      更新时间:2023-10-16

我为这个问题发疯了。我根据教程下载了sqlite3的源代码和预编译文件,用vs编译,得到了sqlite3.libsqlite3.exp文件,放在C: sql文件夹中。然后我在 vscode 中创建项目,并将sqlite3.h放在项目的根目录中,并创建了以下测试代码。

#include "sqlite3.h"
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
#pragma comment(lib, "c:\sql\sqlite3.lib")
int main() {
sqlite3 *db = NULL;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("Student.db", &db);
if (rc) {
fprintf(stderr, "Can't open database: %sn", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
} else
printf("open mydata successfully!n");
sqlite3_close(db);
system("pause");
return 0;
}

我的tasks.json

{
// See https://go.microsoft.com/fwlink/?LinkId=733558 
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "gdb",
"type": "shell",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe",
"-LC:\sql",
// "${fileDirname}\src\Account.cpp",
// "${fileDirname}\src\Person.cpp",
// "${fileDirname}\src\Date.cpp",
// "${fileDirname}\src\System.cpp",
// "${fileDirname}\src\Administrator.cpp",
"-lsqlite3",
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

但是无论我如何修改tasks.json文件,我仍然得到以下结果。我真的太迷茫了。谁能帮我解决这个问题?

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:UsersDrewAppDataLocalTempccUXavQh.o:test.cpp:(.text+0x2e): undefined reference to `sqlite3_open'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:UsersDrewAppDataLocalTempccUXavQh.o:test.cpp:(.text+0x45): undefined reference to `sqlite3_errmsg'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:UsersDrewAppDataLocalTempccUXavQh.o:test.cpp:(.text+0x6d): undefined reference to `sqlite3_close'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:UsersDrewAppDataLocalTempccUXavQh.o:test.cpp:(.text+0x91): undefined reference to `sqlite3_close'
collect2.exe: error: ld returned 1 exit status

我个人下载了sqlite-amalgamation源代码并将其编译为常规源文件。SQLite使它变得非常简单,你应该没有问题 https://www.sqlite.org/download.html

当我不需要时,我不会弄乱静态/动态库。问题可能是由于哪个编译器编译了库(即ms编译器,mingw,clang等(。您可能会混合它们导致错误。我怀疑预编译使用的是 ms 编译器。从内存中,clang可以使用使用ms编译器编译的库(clang取决于它(。Mingw 不依赖于它,也不会使用 MS 编译的库