<html lang="en"> <head> <title>Collect2 - GNU Compiler Collection (GCC) Internals</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Compiler Collection (GCC) Internals"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="prev" href="Fragments.html#Fragments" title="Fragments"> <link rel="next" href="Header-Dirs.html#Header-Dirs" title="Header Dirs"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <!-- Copyright (C) 1988-2015 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with the Invariant Sections being ``Funding Free Software'', the Front-Cover Texts being (a) (see below), and with the Back-Cover Texts being (b) (see below). A copy of the license is included in the section entitled ``GNU Free Documentation License''. (a) The FSF's Front-Cover Text is: A GNU Manual (b) The FSF's Back-Cover Text is: You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development.--> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Collect2"></a> <p> Next: <a rel="next" accesskey="n" href="Header-Dirs.html#Header-Dirs">Header Dirs</a>, Previous: <a rel="previous" accesskey="p" href="Fragments.html#Fragments">Fragments</a>, Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a> <hr> </div> <h2 class="chapter">20 <code>collect2</code></h2> <p>GCC uses a utility called <code>collect2</code> on nearly all systems to arrange to call various initialization functions at start time. <p>The program <code>collect2</code> 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 ‘<samp><span class="samp">.c</span></samp>’ file containing a table of them, compiles it, and links the program a second time including that file. <p><a name="index-g_t_005f_005fmain-5071"></a><a name="index-constructors_002c-automatic-calls-5072"></a>The actual calls to the constructors are carried out by a subroutine called <code>__main</code>, which is called (automatically) at the beginning of the body of <code>main</code> (provided <code>main</code> was compiled with GNU CC). Calling <code>__main</code> is necessary, even when compiling C code, to allow linking C and C++ object code together. (If you use <samp><span class="option">-nostdlib</span></samp>, you get an unresolved reference to <code>__main</code>, since it's defined in the standard GCC library. Include <samp><span class="option">-lgcc</span></samp> at the end of your compiler command line to resolve this reference.) <p>The program <code>collect2</code> is installed as <code>ld</code> in the directory where the passes of the compiler are installed. When <code>collect2</code> needs to find the <em>real</em> <code>ld</code>, it tries the following file names: <ul> <li>a hard coded linker file name, if GCC was configured with the <samp><span class="option">--with-ld</span></samp> option. <li><samp><span class="file">real-ld</span></samp> in the directories listed in the compiler's search directories. <li><samp><span class="file">real-ld</span></samp> in the directories listed in the environment variable <code>PATH</code>. <li>The file specified in the <code>REAL_LD_FILE_NAME</code> configuration macro, if specified. <li><samp><span class="file">ld</span></samp> in the compiler's search directories, except that <code>collect2</code> will not execute itself recursively. <li><samp><span class="file">ld</span></samp> in <code>PATH</code>. </ul> <p>“The compiler's search directories” means all the directories where <samp><span class="command">gcc</span></samp> searches for passes of the compiler. This includes directories that you specify with <samp><span class="option">-B</span></samp>. <p>Cross-compilers search a little differently: <ul> <li><samp><span class="file">real-ld</span></samp> in the compiler's search directories. <li><samp><var>target</var><span class="file">-real-ld</span></samp> in <code>PATH</code>. <li>The file specified in the <code>REAL_LD_FILE_NAME</code> configuration macro, if specified. <li><samp><span class="file">ld</span></samp> in the compiler's search directories. <li><samp><var>target</var><span class="file">-ld</span></samp> in <code>PATH</code>. </ul> <p><code>collect2</code> explicitly avoids running <code>ld</code> using the file name under which <code>collect2</code> itself was invoked. In fact, it remembers up a list of such names—in case one copy of <code>collect2</code> finds another copy (or version) of <code>collect2</code> installed as <code>ld</code> in a second place in the search path. <p><code>collect2</code> searches for the utilities <code>nm</code> and <code>strip</code> using the same algorithm as above for <code>ld</code>. <!-- Copyright (C) 1988-2015 Free Software Foundation, Inc. --> <!-- This is part of the GCC manual. --> <!-- For copying conditions, see the file gcc.texi. --> </body></html>