c - GCC looks for headers in /usr/local/include when compiling, but not for libraries in /usr/local/lib when linking. Why? -
i have installed in /usr/ distribution provided version of sqlite - version 3.4.2. have installed in /usr/local/ sqlite version 3.7.4.
/usr/include/sqlite3.h defines sqlite_version_number 3004002
/usr/local/include/sqlite3.h defines sqlite_version_number 3007004
version 3007004 has function sqlite3_initialize(), version 3004002 not.
$ nm -d /usr/local/lib/libsqlite3.so | grep sqlite3_initialize 00018e20 t sqlite3_initialize
when compile following example program:
#include <stdio.h> #include <sqlite3.h> // should fail if including /usr/include/sqlite3.h #if sqlite_version_number != 3007004 #error "sqlite version not 3.7.4" #endif int main() { printf( "%d\n", sqlite_version_number ); sqlite3_initialize(); return 0; }
when compiled , linked (with gcc 4.2.4) preprocessor finds sqlite3.h header version 3.7.4 in /usr/local/include/, linker fails it's looking in /usr/lib/libsqlite3.so symbols.
$ gcc -wall test.c -o cpp -lsqlite3 /tmp/cc4issn6.o: in function `main': test.c:(.text+0x26): undefined reference `sqlite3_initialize' test.c:(.text+0x2b): undefined reference `sqlite3_shutdown' collect2: ld returned 1 exit status
of course can specify lib directory , links correct version of library.
$ gcc -wall test.c -o cpp -l/usr/local/lib -lsqlite3 $ ./cpp 3007004 $
it seems default gcc looks in /usr/local/include/ before /usr/include/ headers, not libraries when linking. why?
edit 1: suggested tim post:
$ sudo ldconfig -n /usr/local/lib $ ldconfig -p | grep sqlite3 libsqlite3.so.0 (libc6) => /usr/local/lib/libsqlite3.so.0 libsqlite3.so.0 (libc6) => /usr/lib/libsqlite3.so.0 libsqlite3.so (libc6) => /usr/local/lib/libsqlite3.so libsqlite3.so (libc6) => /usr/lib/libsqlite3.so $ gcc -wall cpp.c -o cpp -lsqlite3 /tmp/ccwpt9o0.o: in function `main': cpp.c:(.text+0x26): undefined reference `sqlite3_initialize' cpp.c:(.text+0x2b): undefined reference `sqlite3_shutdown' collect2: ld returned 1 exit status
the include file search path defined gcc, library search path encoded ld, separate project; these not synchronized.
one thing patch specs file, which, if exists, can found in same directory libgcc; can path latter using
gcc -print-libgcc-file-name
if there no specs file there, create 1 using
gcc -dumpspecs >specs
and verify gcc reading it, calling
gcc -v
look line containing %{l*}
, , add -l/usr/local/lib
behind (separated spaces). gcc pass argument following -l options command line ld when linking.
in order restore defaults, revert specs file initial state (i.e. delete if didn't exist before).
Comments
Post a Comment