【发布时间】:2020-07-08 17:43:22
【问题描述】:
我想使用 sqlite3 作为我的 c++ 的数据库接口,所以我决定从这个教程开始:https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm
我已经在我的电脑上安装了 sqlite3 并为其添加了一个环境变量,但是当我尝试按照教程的建议编译一个非常简单的程序时,输入
gcc test.c -l sqlite3
在命令提示符下,我收到以下错误:
test.c:2:10: fatal error: sqlite3.h: No such file or directory
2 | #include <sqlite3.h>
| ^~~~~~~~~~~
compilation terminated.
test.c 文件是我桌面上的一个文件夹。该文件夹具有以下结构:
test.c
shell.c
sqlite3.c
sqlite3.h
sqlite3ext.h
最后四个文件来自我在https://www.sqlite.org/download.html (sqlite-amalgamation-3320300.zip) 上找到的合并 zip
几个小时以来我一直在寻找问题所在,我能找到的唯一可能的解释是编译器无法链接外部 sqlite3 库,但即便如此我也找不到有效的链接方法,任何关于如何我可以编译上面的吗?
test.c
#include <stdio.h>
#include <sqlite3.h>
int main(int argc, char* argv[]) {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
} else {
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}
感谢您提前提供的任何帮助。
编辑:
如果我输入 #include "sqlite3.h" 而不是 #include
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:找不到-lsqlite3 collect2.exe : 错误: ld 返回 1 个退出状态
【问题讨论】:
-
你的编译器告诉你它找不到 sqlite3.h。它在你系统的什么位置?添加 -I 标志以将您的编译器指向它。
-
新错误是您忘记链接到 sqlite 库或在项目中使用
.cAmalgamation 文件。 https://www.sqlite.org/amalgamation.html -
您的错误现在是链接器错误,而不是编译器错误。看来您对 C++(或 C)应用程序的构建过程、编译和链接并不熟悉。
-
您在某处有
libsqlite3.a文件吗?使用-Lyour/path/here将链接器指向您拥有它的目录。 -
如果您不使用它们,仅将文件放在文件夹中对您没有帮助。错误在这里:
gcc test.c -l sqlite3你需要gcc test.c sqlite3.c