Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 kinda weird cause -lfuse is being 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 an error and it seems that this is the way it should be: library after object files

my question is: why is the parameter order relevant for gcc/ld? i tought gcc just parses the params like every other application and may forward the necessary params to ld or such

in general: anyone knows facts or tips for gcc parameter ordering and maybe a bit background information about why it is needed this way?

thanks

like image 879
John Doe Avatar asked Jan 31 '13 10:01

John Doe


People also ask

Does order matter in GCC?

The gcc program accepts options and file names as operands. Many options have multi-letter names; therefore multiple single-letter options may not be grouped: -dr is very different from -d -r. You can mix options and other arguments. For the most part, the order you use doesn't matter.

Does the order of compiler flags matter?

For the most part, the order you use doesn't matter. Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.

What G does in GCC?

-g requests that the compiler and linker generate and retain source-level debugging/symbol information in the executable itself.

How do I use GCC instead of G ++?

The different options of GCC command allow the user to stop the compilation process at different stages. g++ command is a GNU c++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file.


2 Answers

The order of objects and libraries is relevant to the linker (called implicitly by the compiler when creating an executable). When the linker, in its left-to-right scan, finds a use of a name it doesn't know about it starts looking for a definition from that point on. If a definition passes by, it doesn't remember it for later use.

like image 77
vonbrand Avatar answered Oct 11 '22 03:10

vonbrand


GCC itself passes parameters to ld in relatively transparent fashion

Your question really is about how ld linker works. For simplicity and to handle circular references without infinite loops, it passes through the list of libraries only once, resolving references. So if your reference occurs somewhere and it hasn't seen the library that contains it yet then it's just an error.

Also please read this discussion, where this question is discussed in greater details.

like image 25
Konstantin Vladimirov Avatar answered Oct 11 '22 04:10

Konstantin Vladimirov