-   Linux - Software (
-   -   GCC; See what arguments are passed to the linker. (

Zssfssz 03-03-2012 11:59 PM

GCC; See what arguments are passed to the linker.
I am in a situation where I must have an intermediat object file. However when I build the execuable there is error when linking. When having the compiler start the linker for me then it goes fine. Is there an easy (Not Going Through GCC SRC) way to find out what is passed to the linker in terms of arguments. Thanks.

makyo 03-04-2012 11:08 AM



GCC uses a utility called collect2 on nearly all systems to arrange to call various initialization functions at start time.

The program collect2 works by linking the program once and looking through the linker output file for symbols with particular names indicating they are constructor functions. If it finds any, it creates a new temporary `.c' file containing a table of them, compiles it, and links the program a second time including that file.

The actual calls to the constructors are carried out by a subroutine called __main, which is called (automatically) at the beginning of the body of main (provided main was compiled with GNU CC). Calling __main is necessary, even when compiling C code, to allow linking C and C++ object code together. (If you use -nostdlib, you get an unresolved reference to __main, since it's defined in the standard GCC library. Include -lgcc at the end of your compiler command line to resolve this reference.)

The program collect2 is installed as ld in the directory where the passes of the compiler are installed ...

-v Print (on standard error output) the commands executed to
run the stages of compilation. Also print the version number
of the compiler driver program and of the preprocessor and the
compiler proper.

-### Like -v except the commands are not executed and all
command arguments are quoted. This is useful for shell scripts
to capture the driver-generated command lines.

-- excerpt from man gcc, q.v.

A partial output of the -v option from a trivial c code compilation on my system is:

COLLECT_GCC_OPTIONS='-v' '-mtune=generic'
/usr/lib/gcc/x86_64-linux-gnu/4.3.2/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=both -dynamic-linker /lib64/ /usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.3.2/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2 -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2 -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../.. /tmp/ccgpROfj.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.3.2/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/crtn.o
For this environment:

OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny)
gcc (Debian 4.3.2-1.1) 4.3.2

Best wishes ... cheers, makyo

All times are GMT -5. The time now is 08:09 PM.