Mjand
Mjand

Reputation: 51

How to set library search path for 64 bit libraries for g++ in Ubuntu?

Trying to compile something for 64 bit unix using Ubuntu. As a disclaimer I only started using linux and gcc a few days ago so still learning my way around. Anyway, getting the following error:

/home/myuser/myproject/myfile.cpp:437: undefined reference to `clock_gettime'

A quick google reveals I need the -lrt option to link with librt.a. So I check my command line ( formatted for readability, different file names and I've remove lists of file names ):

/usr/bin/g++ 
-Wl,
--gc-sections 
-fno-exceptions 
-m64 
-B/usr/bin 
-o 
"/home/myuser/myproject" 
-Wl,
-Map, "/home/myuser/myproject/myproject.map" 
-g  
"/home/myuser/myproject/myproject.cpp.obj" 
..and some more .objs..
-Xlinker 
--start-group  
"-lpthread"  
"-lrt"
"/home/myuser/myproject/lib/mylib.a" 
..and some more .as..
-Xlinker 
--end-group 

Hmm. Looks like -lrt is already there, maybe I don't have librt.a? Nope searching all files reveals I have /usr/lib/x86_64-linux-gnu/librt.a. I guess g++ is looking in the wrong place. So in the above command line I replace -lrt with /usr/lib/x86_64-linux-gnu/librt.a and bingo! it compiles and links fine. Unfortunately, this is an automated tool and I need this to work on many computers and can't make assumptions about the location of librt.a so I really need it to work with -lrt. So how do I set the local libary search path? First attempt is changing LD_LIBRARY_PATH environment variable but apparently ( from what I can tell from more googling ) this is ignored on ubuntu and instead I should be messing with .conf files in /etc/ld.so.conf.d/, however I already it looks like I already have x86_64-linux-gnu.conf in there with the following lines:

# Multiarch support
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu

From my reading up this point it looks that should be all I need. Kind of stuck as to where to go from here...

Upvotes: 4

Views: 3678

Answers (1)

Mjand
Mjand

Reputation: 51

Answering my own question just in case someone else has this problem. Turns out the correct librt.a was being linked but the linker is very sensitive to the link order. Putting -lrt and -lpthread at the end of the group fixes the problem.

Upvotes: 1

Related Questions