Aamir Ahmed Khan
Aamir Ahmed Khan

Reputation:

GCC is not linking library to a non-default path

I have boost C++ libraries already installed on my Fedora10 machine but I want to use a newer version that I keep at some location in my home folder. I want g++ to use include and library files from my home folder location instead of default (/usr/include and /usr/lib64).

For that matter, I also have declared CPLUS\_INCLUDE\_PATH and LIBRARY\_PATH environment variables in my ~/.bashrc file as explained here.

Now when I run,

g++ -o hello.so -fPIC hello.cpp -shared -lboost_python

The preprocessor uses include files from my home folder location, overriding the default location (as it should, because CPLUS\_INCLUDE\_PATH has a higher precedence in the search path). But the linker does not seem to follow the same precedence rule. It always uses libboost_python.so from the default location /usr/lib64 instead of first searching LIBRARY\_PATH. It only links to the libboost\_python.so library in my home folder when I explicitly specify with -L switch. This is really inconvenient.

Upvotes: 2

Views: 2118

Answers (1)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

The -L switch is the standard way of telling the compiler where to find the libraries. Write a makefile that builds your compiler/linker switches - you'll find it's worth investing your time. You can do something like:

MY_LIBPATH += -L$(BOOST_LIB_PATH)
MY_INCPATH += -I$(BOOST_INC_PATH)
hello.so: hello.cpp
    g++ -o $@ -fPIC $(MY_INCPATH) $(MY_LIBPATH) hello.cpp -shared -lboost_python

And then you can control this via environment (of course there could be many variations on how to structure the makefile.)

Upvotes: 2

Related Questions