190075Sobrien@c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. 290075Sobrien@c This is part of the GCC manual. 390075Sobrien@c For copying conditions, see the file gcc.texi. 490075Sobrien 590075Sobrien@node Collect2 690075Sobrien@chapter @code{collect2} 790075Sobrien 8119256SkanGCC uses a utility called @code{collect2} on nearly all systems to arrange 990075Sobriento call various initialization functions at start time. 1090075Sobrien 1190075SobrienThe program @code{collect2} works by linking the program once and 1290075Sobrienlooking through the linker output file for symbols with particular names 1390075Sobrienindicating they are constructor functions. If it finds any, it 1490075Sobriencreates a new temporary @samp{.c} file containing a table of them, 1590075Sobriencompiles it, and links the program a second time including that file. 1690075Sobrien 1790075Sobrien@findex __main 1890075Sobrien@cindex constructors, automatic calls 1990075SobrienThe actual calls to the constructors are carried out by a subroutine 2090075Sobriencalled @code{__main}, which is called (automatically) at the beginning 2190075Sobrienof the body of @code{main} (provided @code{main} was compiled with GNU 2290075SobrienCC)@. Calling @code{__main} is necessary, even when compiling C code, to 2390075Sobrienallow linking C and C++ object code together. (If you use 2490075Sobrien@option{-nostdlib}, you get an unresolved reference to @code{__main}, 2590075Sobriensince it's defined in the standard GCC library. Include @option{-lgcc} at 2690075Sobrienthe end of your compiler command line to resolve this reference.) 2790075Sobrien 2890075SobrienThe program @code{collect2} is installed as @code{ld} in the directory 2990075Sobrienwhere the passes of the compiler are installed. When @code{collect2} 3090075Sobrienneeds to find the @emph{real} @code{ld}, it tries the following file 3190075Sobriennames: 3290075Sobrien 3390075Sobrien@itemize @bullet 3490075Sobrien@item 3590075Sobrien@file{real-ld} in the directories listed in the compiler's search 3690075Sobriendirectories. 3790075Sobrien 3890075Sobrien@item 3990075Sobrien@file{real-ld} in the directories listed in the environment variable 4090075Sobrien@code{PATH}. 4190075Sobrien 4290075Sobrien@item 4390075SobrienThe file specified in the @code{REAL_LD_FILE_NAME} configuration macro, 4490075Sobrienif specified. 4590075Sobrien 4690075Sobrien@item 4790075Sobrien@file{ld} in the compiler's search directories, except that 4890075Sobrien@code{collect2} will not execute itself recursively. 4990075Sobrien 5090075Sobrien@item 5190075Sobrien@file{ld} in @code{PATH}. 5290075Sobrien@end itemize 5390075Sobrien 5490075Sobrien``The compiler's search directories'' means all the directories where 55117395Skan@command{gcc} searches for passes of the compiler. This includes 5690075Sobriendirectories that you specify with @option{-B}. 5790075Sobrien 5890075SobrienCross-compilers search a little differently: 5990075Sobrien 6090075Sobrien@itemize @bullet 6190075Sobrien@item 6290075Sobrien@file{real-ld} in the compiler's search directories. 6390075Sobrien 6490075Sobrien@item 6590075Sobrien@file{@var{target}-real-ld} in @code{PATH}. 6690075Sobrien 6790075Sobrien@item 6890075SobrienThe file specified in the @code{REAL_LD_FILE_NAME} configuration macro, 6990075Sobrienif specified. 7090075Sobrien 7190075Sobrien@item 7290075Sobrien@file{ld} in the compiler's search directories. 7390075Sobrien 7490075Sobrien@item 7590075Sobrien@file{@var{target}-ld} in @code{PATH}. 7690075Sobrien@end itemize 7790075Sobrien 7890075Sobrien@code{collect2} explicitly avoids running @code{ld} using the file name 7990075Sobrienunder which @code{collect2} itself was invoked. In fact, it remembers 8090075Sobrienup a list of such names---in case one copy of @code{collect2} finds 8190075Sobrienanother copy (or version) of @code{collect2} installed as @code{ld} in a 8290075Sobriensecond place in the search path. 8390075Sobrien 8490075Sobrien@code{collect2} searches for the utilities @code{nm} and @code{strip} 8590075Sobrienusing the same algorithm as above for @code{ld}. 86