【问题标题】:Can not connect sqlite3 with c using mingw on windows无法在 Windows 上使用 mingw 将 sqlite3 与 c 连接
【发布时间】: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 库或在项目中使用 .c Amalgamation 文件。 https://www.sqlite.org/amalgamation.html
  • 您的错误现在是链接器错误,而不是编译器错误。看来您对 C++(或 C)应用程序的构建过程、编译和链接并不熟悉。
  • 您在某处有libsqlite3.a 文件吗?使用-Lyour/path/here 将链接器指向您拥有它的目录。
  • 如果您不使用它们,仅将文件放在文件夹中对您没有帮助。错误在这里:gcc test.c -l sqlite3 你需要gcc test.c sqlite3.c

标签: c++ c sqlite mingw


【解决方案1】:

gcc test.c -l sqlite3

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

在 cmets 中,您确实有合并文件 sqlite.c。相关文档在这里:https://www.sqlite.org/amalgamation.html

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

gcc test.c sqlite3.c -o myexecutable

【讨论】:

  • 我在 linux 上尝试了类似的东西:x86_64-w64-mingw32-gcc pflanzen.cpp sqlite3.c -o herbarium.exe 在 linux 上,但它不起作用。我不确定这是否是类似的问题。该文件在这里称为 sqlite3.c。
  • 这对我来说非常有用,谢谢,赞成
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 2016-12-20
  • 1970-01-01
  • 2018-11-22
相关资源
最近更新 更多