why this option did not work as a compiler option but only as linker option
A compiler translates your C code to machine code. The compiler does not care what is in other libraries - it only cares about the source file. Let's say the compiler generates a machine code with a list of symbols and locations of these symbols inside source file. Many symbols may be undefined here. So like a foo();
call is translated to like CALL foo
(this is an oversimplification).
The linker merges multiple object files into an executable. So it finds all symbols and if you have CALL foo
in main.o
and a library foo.o
with symbol foo
, it changes it to CALL 0x12345678
- with the location of the foo
function in the final executable (ie. linker can re-order symbols as he sees fit) (this super oversimplification).
The point is, the compiler does not find any libraries. Linker does. So the option has to be passed to the linker.
GCC is called a "compiler" as a whole, in the sense it comes with a number of tools - preprocessor, compiler and linker (see https://unix.stackexchange.com/questions/77779/relationship-between-cc1-and-gcc ).