Gcc / g ++ parameter order

I just compiled chironfs on my new ubuntu 12.10 server and got the following error:

gcc  -Wall -W -Wmissing-prototypes -g -O2 -DFUSE_USE_VERSION=25 -D_FILE_OFFSET_BITS=64 -I/usr/local/include -g -O2 -lm -lfuse  -o chironfs chironfs.o chiron-conf.o chirondbg.o chironfn.o  
chironfs.o: In function `chiron_init':
/root/chironfs-1.1.1/src/chironfs.c:2000: undefined reference to `pthread_create'
chironfs.o: In function `get_rights_by_name':
/root/chironfs-1.1.1/src/chironfs.c:452: undefined reference to `fuse_get_context'

      

the pthread error tells me that -lpthread is missing, but the fuse error is a strange reason - lfuse is used

i found a solution here which suggests to put libraries after object files

so I removed -lfuse and added -lfuse -lpthread at the very end of the line

now it compiles without error and it seems like it should be: library after object files

my question is, why does the order of the parameters matter for gcc / ld? i tought gcc just parses the parameters like any other application and can redirect the required parameters to ld or such

in general: does anyone know facts or hints for ordering gcc options and maybe a little background information on why this is needed this way?

thank

+3


source to share


2 answers


The order of objects and libraries is relative to the linker (implicitly by the compiler when the executable is created). When the linker in its left-right view finds a use of a name that it doesn't know about, it starts looking for the definition at that point. If the definition passes, it is not remembered for later use.



+3


source


GCC itself passes parameters to ld in a relatively transparent manner

Your question is really about how the ld linker works. For simplicity and handling circular references without endless loops, it goes through the library list once, resolving the references. So if your link is happening somewhere and it hasn't seen the library that contains it yet, then it's just a bug.



Also please read this discussion where this issue is discussed in more detail.

+2


source







All Articles