Search code examples
buildlinkerg++librariesinclude-path

Building with either the system version or a local version of a library


I need a build mechanism which can be abstractly described like this:

  • compile and link the code with the system version of a library, or
  • compile and link the code with a local version of the same library

In other words:

I use a library in my program and I need to test more than one version of that library. I need to test the system version, installed in the system directories, but also older or newer versions of the same library that I downloaded, built and installed myself in local directories.

My current methodology: depending on the user command I either leave -I and -L empty or set them to point to the local directories of the chosen version of the library.

When I use the system version it works OK, since the compiler's default -I and -L search paths point to the system version of the library.

But when I select a local version, depending on the compilation/link order (which is at least hard to figure out) the build can happen against the system or the local library, depending on which one the compiler finds first.

Moreover, when I run the program, I need to set LD_LIBRARY_PATH to the local library directory if I want to test a local library version.

Is there a clean way to build and then run with either the system or local versions of the library?

In order to avoid this non-deterministic behaviour I can link with the .a files directly (avoiding -L search paths) but I can still have problems with the include paths, since the compilation can be using a system version header or a local one silently.

I'm using scons with g++ if that helps.

Is there an alternative method?

Basically I need to do:

scons
./a.out

(runs with system version of the library)

scons library=1.0
./a.out

(runs with version 1.0 of the library)

scons library=3.0
./a.out

(runs with version 3.0 of the library)


Solution

  • I think this can be solved like this:

    If compiling against the local library version use -I<include path>, -L<library path> and -Wl,-rpath=<library path>.

    Otherwise use the default system settings.

    From what I've learned from GCC's IRC channel, -I and -L paths should take prevalence over system paths. Also, check http://www.scons.org/wiki/UsingOrigin for an explanation on using rpath (and $ORIGIN) with SCons.