无法在 windows 上使用 mingw 将 sqlite3 与 c 连接

Can not connect sqlite3 with c using mingw on windows

我想使用 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

几个小时以来我一直在寻找问题所在,我能找到的唯一可能的解释是编译器无法 link 外部 sqlite3 库,但即使那样我也找不到 linking 有效的方法,关于如何编译以上内容的任何想法?

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个退出状态

gcc test.c -l sqlite3

您正在尝试 link 到您没有的 libsqlite3.a 文件。

从评论中您确实得到了合并文件 sqlite.c。相关文档在这里:https://www.sqlite.org/amalgamation.html

一个简单的使用方法是您可以像这样用您的可执行文件构建它:

gcc test.c sqlite3.c -o myexecutable