birmingham@ic.fbi.gov , phoenix@ic.fbi.gov , fbise@leo.gov
NickSoft Linux Cookbook Index NickSoft Linux Cookbook
Quick howto's, Real live examples.
 
 FAQFAQ   SearchSearch   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Libraries path or link to shared libraries.

 
Post new topic   Reply to topic    NickSoft Linux Cookbook Index -> Linux Programming
View previous topic :: View next topic  
Author Message
NickSoft
Site Admin


Joined: 13 Nov 2006
Posts: 22

PostPosted: Mon May 21, 2007 12:45 pm    Post subject: Libraries path or link to shared libraries. Reply with quote

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:
Code:
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:
Code:
LD_RUN_PATH=/usr/local/path-to-lib make

or
Code:
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:
Code:
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:
Code:
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)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    NickSoft Linux Cookbook Index -> Linux Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




MLDb.org Magic Eight Ball Croler Web Search Croler Web Directory Bianca Ryan MyBestMatch.net Microlab.info

Powered by 220V