views:

98

answers:

3

I'm having trouble getting a sample program to link correctly (in this case against the ICU library). When I do 'make', everything builds fine. But when I run it, it says it can't find one of the .so's. I double checked they're all installed in /usr/local/lib. What I discovered was it was looking in /usr/lib. If I symlink from there to there actual location, it works.

Why is my LIBPATHS being ignored or not used?

Here is the Makefile

CC = g++

INCPATHS = -I/usr/local/include

CFLAGS = -c -Wall $(INCPATHS)

LIBPATHS = -L/usr/local/lib/
LIBS = $(LIBPATHS) -licuio -licui18n -licuuc -licuio -licudata

EXECUTABLE = prog

print_linking = echo -e "\033[32m" "Linking: $<" "\033[39m"
print_compiling = echo -e "\033[33m" "Compiling: $<" "\033[39m"
print_cleaning = echo -e "\033[31m" "Cleaning: `pwd`" "\033[39m"


all: main

# [target]: [dependencies]
# <tab> system command
main: main.o
    @$(print_linking)
    @$(CC) -o $(EXECUTABLE) main.o $(LIBS) >> /dev/null

main.o: main.cpp
    @$(print_compiling)
    @$(CC) $(CFLAGS) main.cpp 

clean:
    @$(print_cleaning)
    @rm -rf *.o *~ $(EXECUTABLE)
+1  A: 

The path to the dynamic libraries isn't stored in the executable by default. You can either:

  • use LD_LIBRARY_PATH at runtime to give a path where to search for dynamic libraries

  • use -Wl,-R*path* at link time to store a path in the executable

AProgrammer
+1  A: 

One solution is to add /usr/local/lib to the environment variable LD_LIBRARY_PATH. You can do this in your .profile or .cshrc

You can also get the linker to store the full path to the library in the executable.

Both solutions have different tradeoffs with respect to using the execultable by different users and/or on different machines.

Andrew Stein
+2  A: 

Your LIBPATHS tells the linker where to find the library when linking to resolve symbols.

At runtime, you need to tell the loader where to find the library. It doesn't know about what happened at compile time. You can use the LD_LIBRARY_PATH variable as mentioned above, or check into /etc/ld.so.conf and it's friends.

John Ledbetter