SQLite Forum

What am I doing wrong to get errors when trying to compile amalgamation file in Linux
Login

What am I doing wrong to get errors when trying to compile amalgamation file in Linux

(1) By Gary (1codedebugger) on 2021-06-01 06:43:16 [link]

Hello, I'm attempting to compile a C file with the sqlite3 amalgamation and get the following errors.  I tried on versions 3.34.0 and 3.35.5 and am using Manjaro Linux.  I'm certain that I compiled the same on Windows 7 without any issue; and am sure the cause is that I don't know something I should about Linux.

Would you please let me know what I'm doing wrong?  Thank you.

$ gcc -O2 -c sqlite3.c -- generates the sqlite3.o file without error.

$ gcc -O2 sqlite3.o myfile.c -o myfile.exe

/usr/bin/ld: sqlite3.o: in function `pthreadMutexTry':
sqlite3.c:(.text+0x3c65): undefined reference to `pthread_mutex_trylock'
/usr/bin/ld: sqlite3.o: in function `sqlite3ThreadCreate':
sqlite3.c:(.text+0x867c): undefined reference to `pthread_create'
/usr/bin/ld: sqlite3.o: in function `vdbeSorterJoinThread':
sqlite3.c:(.text+0x8718): undefined reference to `pthread_join'
/usr/bin/ld: sqlite3.o: in function `pthreadMutexAlloc':
sqlite3.c:(.text+0x12ed6): undefined reference to `pthread_mutexattr_init'
/usr/bin/ld: sqlite3.c:(.text+0x12ee3): undefined reference to `pthread_mutexattr_settype'
/usr/bin/ld: sqlite3.c:(.text+0x12ef6): undefined reference to `pthread_mutexattr_destroy'
/usr/bin/ld: sqlite3.o: in function `unixDlError':
sqlite3.c:(.text+0x29e2f): undefined reference to `dlerror'
/usr/bin/ld: sqlite3.o: in function `sqlite3VdbeExec':
sqlite3.c:(.text+0x830ba): undefined reference to `pthread_join'
/usr/bin/ld: sqlite3.o: in function `unixDlClose':
sqlite3.c:(.text+0x4674): undefined reference to `dlclose'
/usr/bin/ld: sqlite3.o: in function `unixDlSym':
sqlite3.c:(.text+0x4687): undefined reference to `dlsym'
/usr/bin/ld: sqlite3.o: in function `unixDlOpen':
sqlite3.c:(.text+0x4699): undefined reference to `dlopen'
collect2: error: ld returned 1 exit status

(2) By Gunter Hick (gunter_hick) on 2021-06-01 06:54:03 in reply to 1 [link]

You are missing the pthread and the dynamic load libraries required by SQLite in the link phase of the second command. Add -lpthread and -ldl

(3) By Larry Brasfield (larrybr) on 2021-06-01 10:02:47 in reply to 1

See <u>[How to Compile the CLI](https://sqlite.org/howtocompile.html#cli)</u>, where Gunter's advice is confirmed.

The <u>[Permuted Document Title Index](https://sqlite.org/sitemap.html)</u> would have led to that doc with a link found using the word "compile".

(4) By Gary (1codedebugger) on 2021-06-01 14:49:07 in reply to 2 [link]

Thank you.  Does this apply to Linux only?  I ask because I just compiled the same on Windows 7 from the sqlite3.o made in 3.34.0 without these flags and it worked without error.  I haven't used the amalgamation in about six months, and thought maybe I had forgotten that I had used these flags before but it appears I didn't use them.

I'm pretty sure the only thing used to compile to sqlite3.o was -DSQLITE_ENABLE_JSON1.

Although I'm not attempting to compile the CLI because the Manjaro software installer did that for me, that note given but Larry Brasfield mentions the flags for Windows when using mingw+mysys.  I'm using mingw-w64 but don't need those flags.

What am I still not understanding?  Thanks.

(5) By Keith Medcalf (kmedcalf) on 2021-06-01 19:18:59 in reply to 4 [link]

Theoretically you do not need those on Windows (with a MinGW compiler) because those functions (dynamic loading and threading) are natively handled by the NT Kernel (KERNEL32.DLL trampoline) and thus do not need to be present in the link since the Kernel Runtime is always present and a compiler designed to produce "native" Win32 code does not require runtime support for those functions.

You will note that -mthreads (enables thread exception propagation to the main thread) does require runtime library support, but in some versions of MinGW that support code is hauled in automatically during linking and depending on the version of MinGW may always be present even if not specified (since such propagation is a native Win32 function to some extent).

On Linux however, the threading and dynamic linking routines are runtime functions and not native functions (as in the compiler does not generate the syscalls directly), therefore you must haul in the runtime support libraries.

(6) By Gary (1codedebugger) on 2021-06-02 02:14:53 in reply to 5 [link]

Thank you for the explanation; it's very helpful to me and I appreciate it.