Add the location of a shared library when compiling

Shared libraries are usually loaded after a program starts running. If you are using gcc (the Gnu Compiler Collection), you can add a path to the directory where you can find a shared library such as libsome.so, use the compiler flag --rpath as shown in the following makefile

DIR=/arbitrary/directory/somewhere
LDFLAGS=-Wl,--rpath -Wl,$(DIR)

some:   some.c
        gcc -o $@ some.c -L $(DIR) -l some $(LDFLAGS)
to compile the output so that at runtime it looks in /arbitrary/directory/somewhere for the libsome.so library. Note here the -Wl flag is a wrapper which tells the compiler to pass the following thing directly to the linker.
Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer