3

I have a custom build of SQLite3 at /somepath, so /somepath/.libs contains libsqlite3.so.0.8.6 and the symbolic links to it. I wanted to link a program against it and assumed

g++ -O3 -g -fPIC -I /somepath -I /somepath/src -L /somepath/.libs -lsqlite3 -o myfile.so myfile.cpp

would work. It compiles, but I get a segmentation fault due to some problem in my code, and when trying to debug I run into the issues which look like LD_PRELOAD not working with my program and Setting my lib for LD_PRELOAD makes some processes produce loader errors: I can run LD_PRELOAD=myfile.so /somepath/sqlite3 ..., but under GDB I get symbol lookup error and LD_DEBUG=all LD_PRELOAD=myfile.so gdc -c core /somepath/sqlite3 ... reveals symbols are getting looked up in /usr/lib/x86_64-linux-gnu/libsqlite3.so.0 instead of /somepath/libsqlite3.so.0, and unsurprisingly missing the symbols for functions added in the custom build. How can I fix this and debug my code?

Community
  • 1
  • 1
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 3
    The title contradicts the question. Are you asking on how to make gdb to load your library or gcc to link with your library? – SergeyA Dec 23 '16 at 14:51
  • @SergeyA What I _think_ is that gcc isn't linking to the library I want, but I could be misunderstanding. – Alexey Romanov Dec 23 '16 at 17:14

2 Answers2

6

The -lsqlite3 argument should be last. Order of arguments to g++ matters a lot. You should read more about runpath and perhaps pass -Wl,-rpath,/somepath/.libs

You may want to pass -v once to g++ to understand what is happening (what programs are actually running). You might also pass -Wl,--verbose to ask a more verbose link.

Then you can use ldd on your executable (and also readelf) to find out more what are its link time dependencies.

With suitable arguments to g++ you should not need additional options to gdb

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

From http://visualgdb.com/gdbreference/commands/set_solib-search-path

Inside gdb use the commands below.

set solib-search-path [Directories]
show solib-search-path
JimmyNJ
  • 1,134
  • 1
  • 8
  • 23