0

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?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621

1 Answers1

1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I tried it and I got this: lib_sqlite3.o: In function `pthreadMutexAlloc': lib_sqlite3.c:(.text+0x4137): undefined reference to `pthread_mutexattr_init' lib_sqlite3.c:(.text+0x4148): undefined reference to `pthread_mutexattr_settype' lib_sqlite3.c:(.text+0x4167): undefined reference to `pthread_mutexattr_destroy' and more but I can't paste all. –  Sep 22 '19 at 12:32
  • Also using -L. should have included the library path –  Sep 22 '19 at 12:34
  • @MohammedFawaz You have to compile and link with `-pthread` and link with `-ldl` and possibly some other libraries. See https://www.sqlite.org/howtocompile.html – Shawn Sep 22 '19 at 12:40