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
or
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) clean: rm -f *.o cut out $(EXEC)