I want to build this process on an embedded Linux without installing sqlite3 or sqlite3-dev (I already tried to install them and it worked out).
I've 4 files in the directory : main.cpp sqlite3.c sqlite3.h example.db
I included the sqlite3.h in the main.cpp this way:
extern "C"{
#include "sqlite3.h"
}
Then I typed those commands :
gcc -c sqlite3.c -o sqlite3.o
g++ -c main.cpp -o main.o
and it was already so far, then I wrote this
g++ -o main.out main.o -L.
but I'm getting those errors
main.o: In function `main':
main.cpp:(.text+0xf6): undefined reference to `sqlite3_open'
main.cpp:(.text+0x16d): undefined reference to `sqlite3_exec'
main.cpp:(.text+0x1c6): undefined reference to `sqlite3_exec'
main.cpp:(.text+0x1f7): undefined reference to `sqlite3_free'
main.cpp:(.text+0x25c): undefined reference to `sqlite3_exec'
main.cpp:(.text+0x299): undefined reference to `sqlite3_exec'
main.cpp:(.text+0x2ca): undefined reference to `sqlite3_free'
main.cpp:(.text+0x32f): undefined reference to `sqlite3_exec'
main.cpp:(.text+0x33b): undefined reference to `sqlite3_close'
collect2: error: ld returned 1 exit status
How to statically link those files?
You're not actually linking with the SQLite object file sqlite3.o
.
The linker doesn't know about files or libraries that aren't specified explicitly, so you need to do e.g.
g++ -o main.out main.o sqlite3.o
Considering the other error you get, you need to build with the -pthread
option, both when compiling and when linking.
And the -L
option is to add a path that the library searches for libraries you name with the -l
(lower-case L) option. The linker will not automatically search for any libraries or object files. You really need to specify them explicitly when linking.
To summarize, build like this:
g++ -Wall -pthread main.cpp -c
gcc -Wall -pthread sqlite3.c -c
g++ -pthread -o main.out main.o sqlite3.o -ldl
Note that we now also link with the dl
library, as specified in the documentation linked to by Shawn.