Translations of this page:

Libraries path or link to shared libraries

In all howtos I've found they say linking is so easy, but it never works if the library is not in default system paths. Why? You use all options from the howto and it won't run - shared library is not found. That's because they always forgot one option -rpath. It is a linker option and gcc doesn't understand it. I heard that on free BSD gcc understands corresponding option -R and passes it to the linker, but I never tested it.You can try this on BSD:

gcc -L/usr/local/path-to-lib -R/usr/local/path-to-lib -o program program.c -lmylib

Note that this won't work on linux!!!

On linux there are 2 ways:

1. if you compile other software and you don't want to change makefile use LD_RUN_PATH environment variable:

LD_RUN_PATH=/usr/local/path-to-lib make


export LD_RUN_PATH=/usr/local/path-to-lib
gcc -o myprogram myprogram.c

2. Use -Wl (l is small L) to pass options to linker:

gcc -L/usr/local/path-to-lib -Wl,-rpath,/usr/local/path-to-lib -o program program.c -lmylib

So simple makefile should look like:

CC = gcc
CFLAGS = -O2 -Wall -I/usr/local/mysql/include/mysql
LDFLAGS = -Wl,-rpath,/usr/local/mysql/lib/mysql -L/usr/local/mysql/lib/mysql -lmysqlclient

OBJECTS   =   myprogram.o

TARGETS = myprogram
EXEC =   myprogram

all:      $(TARGETS)

install:   all
   cp $(EXEC) /usr/local/bin/myprogram

myprogram:   $(OBJECTS)
   $(CC) -s -o $(EXEC) $(LDFLAGS) $(OBJECTS)

   rm -f *.o cut out $(EXEC)
lcb/programming/run-path.txt · Last modified: 09.22.2008 14:07 by npelov
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki