Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC Not linking correct libraries

I have two versions of GCC installed on my system 4.6.2 and 4.7.0. I am running Fedora Core 16.

4.6.2 is installed in /usr/bin and 4.7.0 is installed in /home/nerozehl/local/bin. The libraries and runtime for C++ are also compiled and installed in /home/nerozehl/local/lib and /home/nerozehl/local/lib64.

I also have two versions of Boost installed, with libraries in /usr/lib64 and /home/nerozehl/local/lib. (Boost 1.47.0 and 1.49.0, respectively)

The problem I am having is that g++ / ld are linking against the default libraries, and not the newer ones in /home/nerozehl/local. I am using configure to generate Makefiles, and am calling it this way:

CXX=g++47 CXXFLAGS="-g -O0 -pg" LDFLAGS="-L/home/nerozehl/local/lib" ./configure --prefix=/home/nerozehl/local

Where g++47 resides in the /home/nerozehl/local/bin (in my $PATH).

When I compile, everything is fine, and the newer headers are used, but when I check what it was linked against:

ldd source/noes
    linux-vdso.so.1 =>  (0x00007fffebfff000)
    libboost_filesystem-mt.so.1.47.0 => /usr/lib64/libboost_filesystem-mt.so.1.47.0 (0x0000003c6a800000)
    libboost_system-mt.so.1.47.0 => /usr/lib64/libboost_system-mt.so.1.47.0 (0x0000003c6a400000)
    libboost_program_options-mt.so.1.47.0 => /usr/lib64/libboost_program_options-mt.so.1.47.0 (0x0000003c6ac00000)
    libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003c6dc00000)
    libm.so.6 => /lib64/libm.so.6 (0x0000003c68c00000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003c69c00000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003c68800000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c69000000)
    librt.so.1 => /lib64/librt.so.1 (0x0000003c69800000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003c68400000)

For the life of me I can't figure out how to force g++ / ld / configure to use my newer libraries. Any help would be appreciated.

like image 749
nerozehl Avatar asked Mar 03 '12 20:03

nerozehl


People also ask

Which GCC option is used to link the library?

The -l option tells GCC to link in the specified library.

Can GCC perform linking?

Linking is performed when the input file are object files " .o " (instead of source file " . cpp " or " . c "). GCC uses a separate linker program (called ld.exe ) to perform the linking.

Does order of linking libraries matter?

When linking object files (static libraries) into an executable, the order in which you give the libraries matters. For simple scenarios where there are no cyclic references, the dependent library should come on the left, and the library which provides said dependency should come on the right.

Where does GCC look for libraries?

GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat.


2 Answers

ldd doesn't tell you what the executable was linked against -- it tells you what shared objects the executable will load when it's run. If you want it to load from /home/nerozehl when it runs, you need to do one of several things:

  • set LD_LIBRARY_PATH to contain /home/nerozehl/local/lib when you run the program

  • add /home/nerozehl/local/lib to ld.so.conf so it will get used by everyone. Only works on systems (such as linux) that use ld.so.conf, however.

  • link the program with -Wl,-rpath,/home/nerozehl/local/lib. Only works on systems that use ELF or another executable format that supports it, however. It also hardcodes the path into the executable, which is somewhat fragile -- if you move the executable to another machine or rearrange your filesystem it may break.

like image 101
Chris Dodd Avatar answered Oct 23 '22 09:10

Chris Dodd


Are you certain your configure script is paying attention to LDFLAGS? Run ./configure --help and see the options. There is usually one called something like --with-boost= and then you give the directory where boost is located. Try that one instead. Similarly for any other options you are having trouble with.

like image 39
darth happyface Avatar answered Oct 23 '22 11:10

darth happyface