1
votes

I want to use sqlite3 as a database interface for my c++, so i decided to start from this tutorial: https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm

I have installed sqlite3 on my computer and add a environment variable for it, but when I try to compile a very simple program as the tutorial suggest by typing

gcc test.c -l sqlite3

on command prompt, I get the following error:

   test.c:2:10: fatal error: sqlite3.h: No such file or directory
    2 | #include <sqlite3.h>
      |          ^~~~~~~~~~~
compilation terminated.

The test.c file is a folder on my desktop. The folder has the following structure:

test.c
shell.c
sqlite3.c
sqlite3.h
sqlite3ext.h

The last four files are from the amalgamation zip I found on the https://www.sqlite.org/download.html (sqlite-amalgamation-3320300.zip)

I have been looking on what is wrong for hours, and the only possible explanation i can find is that the compiler cant link the external sqlite3 library, but even then I could not find a linking method that works, any ideas on how I can compile the above?

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);
}

I appreciate any help in advance.

Edit:

if I type #include "sqlite3.h" instead of #include <sqlite3.h> I get a different error:

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lsqlite3 collect2.exe: error: ld returned 1 exit status

1
Your compiler is telling you it cannot find sqlite3.h. Where on your system is it? Add the -I flag to point your compiler to it. - Botje
The new error is you forgot to link to a sqlite library or use the .c Amalgamation file in your project. https://www.sqlite.org/amalgamation.html - drescherjm
Your error is now a linker error, not a compiler error. It looks like you're not familiar with the build process of a C++ (or C) application, compilation and linking. - PaulMcKenzie
Do you have a libsqlite3.a file somewhere? Use -Lyour/path/here to point the linker to the directory where you have it. - HolyBlackCat
Just having files in a folder will not help you if you don''t use them. The bug is here: gcc test.c -l sqlite3 You need gcc test.c sqlite3.c - drescherjm

1 Answers

2
votes

With

gcc test.c -l sqlite3

you are attempting to link to a libsqlite3.a file which you don't have.

From the comments you do have the Amalgamation file sqlite.c. The documentation for this is here: https://www.sqlite.org/amalgamation.html

One simple way to use this is you can just build it with your executable like this:

gcc test.c sqlite3.c -o myexecutable