Linux

Find a library on the system

There seems to be multiple ways to do this, and sometimes one command works over another, not sure why.

$ ldconfig -p | grep "name-of-lib"
$ dpkg -L "name-of-lib"

Requires installing apt-file

$ apt-file search "name-of-lib

ldd - print shared object dependencies. Very useful for debugging missing shared libraries.

$ ldd $(which curl)

Can also try grep with ls -R

$ ls -R | grep file

Then there is find

$ find . -name "*sql*"

Building on linux

Linking static libraries into shared

When passing arguments to the linker you need to ensure you use the -Wl,–whole-archive and -Wl,–no-whole-archive option. Wrap these around static libraries that you are tyring to pull into a shared library.

-Wl,--whole-archive
-lxml2
-Wl,--no-whole-archive

This is necessary to tell the linker to pull all the functions from the library into the shared library you are building. Otherwise, only some will be pulled in and you will get a linker error.

It seems there is also another way here

Use -l: instead of -l. For example -l:libXYZ.a to link with libXYZ.a. Notice the lib written out, as opposed to -lXYZ which would auto expand to libXYZ.

Note, these commands can be embedded into a CMake script by passing to TARGET_LINK_LIBRARIES

TARGET_LINK_LIBRARIES(target SHARED -W,l--whole-archive l:xml2 -Wl,no-whole-archive)

Inspecting broken builds

List all the shared object libraries that libx depends on

ldd libx.so

List the symbols in a library, along with their status (found, undefined etc.)

nm libx.so

Use the -D option to inspect dynamic symbols only

nm libx.so

Pipe output of nm into grep to search for specific function

nm libx.so | grep somefunction

You can examine the Rpath on Linux thus:

readelf -d libsemsim.so