<p>Why does the order in which libraries are linked sometimes cause errors in GCC?</p>

[{'answer_id': 45206, 'author': 'titanae', 'author_id': 2387, 'author_profile': 'https://Stackoverflow.com/users/2387', 'pm_score': 2, 'selected': False, 'text': '<p>I have seen this a lot, some of our modules link in excess of a 100 libraries of our code plus system &amp; 3rd party libs. </p>\n\n<p>Depending on different linkers HP/Intel/GCC/SUN/SGI/IBM/etc you can get unresolved functions/variables etc, on some platforms you have to list libraries twice. </p>\n\n<p>For the most part we use structured hierarchy of libraries, core, platform, different layers of abstraction, but for some systems you still have to play with the order in the link command.</p>\n\n<p>Once you hit upon a solution document it so the next developer does not have to work it out again.</p>\n\n<p>My old lecturer used to say, "<em>high cohesion &amp; low coupling</em>", it’s still true today.</p>\n'}, {'answer_id': 409402, 'author': 'casualcoder', 'author_id': 10578, 'author_profile': 'https://Stackoverflow.com/users/10578', 'pm_score': 7, 'selected': False, 'text': '<p>The GNU ld linker is a so-called smart linker.  It will keep track of the functions used by preceding static libraries, permanently tossing out those functions that are not used from its lookup tables.  The result is that if you link a static library too early, then the functions in that library are no longer available to static libraries later on the link line.  </p>\n\n<p>The typical UNIX linker works from left to right, so put all your dependent libraries on the left, and the ones that satisfy those dependencies on the right of the link line.  You may find that some libraries depend on others while at the same time other libraries depend on them.  This is where it gets complicated.  When it comes to circular references, fix your code!</p>\n'}, {'answer_id': 409470, 'author': 'Johannes Schaub - litb', 'author_id': 34509, 'author_profile': 'https://Stackoverflow.com/users/34509', 'pm_score': 9, 'selected': False, 'text': "<p><sub>(See the history on this answer to get the more elaborate text, but I now think it's easier for the reader to see real command lines).</sub></p>\n<hr />\n<p>Common files shared by all below commands</p>\n<pre><code>// a depends on b, b depends on d\n$ cat a.cpp\nextern int a;\nint main() {\n  return a;\n}\n\n$ cat b.cpp\nextern int b;\nint a = b;\n\n$ cat d.cpp\nint b;\n</code></pre>\n<h1>Linking to static libraries</h1>\n<pre><code>$ g++ -c b.cpp -o b.o\n$ ar cr libb.a b.o\n$ g++ -c d.cpp -o d.o\n$ ar cr libd.a d.o\n\n$ g++ -L. -ld -lb a.cpp # wrong order\n$ g++ -L. -lb -ld a.cpp # wrong order\n$ g++ a.cpp -L. -ld -lb # wrong order\n$ g++ a.cpp -L. -lb -ld # right order\n</code></pre>\n<p>The linker searches from left to right, and notes unresolved symbols as it goes. If a library resolves the symbol, it takes the object files of that library to resolve the symbol (b.o out of libb.a in this case).</p>\n<p>Dependencies of static libraries against each other work the same - the library that needs symbols must be first, then the library that resolves the symbol.</p>\n<p>If a static library depends on another library, but the other library again depends on the former library, there is a cycle. You can resolve this by enclosing the cyclically dependent libraries by <code>-(</code> and <code>-)</code>, such as <code>-( -la -lb -)</code> (you may need to escape the parens, such as <code>-\\(</code> and <code>-\\)</code>). The linker then searches those enclosed lib multiple times to ensure cycling dependencies are resolved. Alternatively, you can specify the libraries multiple times, so each is before one another: <code>-la -lb -la</code>.</p>\n<h1>Linking to dynamic libraries</h1>\n<pre><code>$ export LD_LIBRARY_PATH=. # not needed if libs go to /usr/lib etc\n$ g++ -fpic -shared d.cpp -o libd.so\n$ g++ -fpic -shared b.cpp -L. -ld -o libb.so # specifies its dependency!\n\n$ g++ -L. -lb a.cpp # wrong order (works on some distributions)\n$ g++ -Wl,--as-needed -L. -lb a.cpp # wrong order\n$ g++ -Wl,--as-needed a.cpp -L. -lb # right order\n</code></pre>\n<p>It's the same here - the libraries must follow the object files of the program. The difference here compared with static libraries is that you need not care about the dependencies of the libraries against each other, because <em>dynamic libraries sort out their dependencies themselves</em>.</p>\n<p>Some recent distributions apparently default to using the <code>--as-needed</code> linker flag, which enforces that the program's object files come before the dynamic libraries. If that flag is passed, the linker will not link to libraries that are not actually needed by the executable (and it detects this from left to right). My recent archlinux distribution doesn't use this flag by default, so it didn't give an error for not following the correct order.</p>\n<p>It is not correct to omit the dependency of <code>b.so</code> against <code>d.so</code> when creating the former. You will be required to specify the library when linking <code>a</code> then, but <code>a</code> doesn't really need the integer <code>b</code> itself, so it should not be made to care about <code>b</code>'s own dependencies.</p>\n<p>Here is an example of the implications if you miss specifying the dependencies for <code>libb.so</code></p>\n<pre><code>$ export LD_LIBRARY_PATH=. # not needed if libs go to /usr/lib etc\n$ g++ -fpic -shared d.cpp -o libd.so\n$ g++ -fpic -shared b.cpp -o libb.so # wrong (but links)\n\n$ g++ -L. -lb a.cpp # wrong, as above\n$ g++ -Wl,--as-needed -L. -lb a.cpp # wrong, as above\n$ g++ a.cpp -L. -lb # wrong, missing libd.so\n$ g++ a.cpp -L. -ld -lb # wrong order (works on some distributions)\n$ g++ -Wl,--as-needed a.cpp -L. -ld -lb # wrong order (like static libs)\n$ g++ -Wl,--as-needed a.cpp -L. -lb -ld # &quot;right&quot;\n</code></pre>\n<p>If you now look into what dependencies the binary has, you note the binary itself depends also on <code>libd</code>, not just <code>libb</code> as it should. The binary will need to be relinked if <code>libb</code> later depends on another library, if you do it this way. And if someone else loads <code>libb</code> using <code>dlopen</code> at runtime (think of loading plugins dynamically), the call will fail as well. So the <code>&quot;right&quot;</code> really should be a <code>wrong</code> as well.</p>\n"}, {'answer_id': 798086, 'author': 'David Cournapeau', 'author_id': 11465, 'author_profile': 'https://Stackoverflow.com/users/11465', 'pm_score': 2, 'selected': False, 'text': '<p>Link order certainly does matter, at least on some platforms. I have seen crashes for applications linked with libraries in wrong order (where wrong means A linked before B but B depends on A).</p>\n'}, {'answer_id': 6717376, 'author': 'Lumi', 'author_id': 269126, 'author_profile': 'https://Stackoverflow.com/users/269126', 'pm_score': 6, 'selected': False, 'text': "<p>Here's an example to make it clear how things work with GCC when <strong>static</strong> libraries are involved. So let's assume we have the following scenario:</p>\n\n<ul>\n<li><code>myprog.o</code> - containing <code>main()</code> function, dependent on <code>libmysqlclient</code></li>\n<li><code>libmysqlclient</code> - static, for the sake of the example (you'd prefer the shared library, of course, as the <code>libmysqlclient</code> is huge); in <code>/usr/local/lib</code>; and dependent on stuff from <code>libz</code></li>\n<li><code>libz</code> (dynamic)</li>\n</ul>\n\n<p>How do we link this? (Note: examples from compiling on Cygwin using gcc 4.3.4)</p>\n\n<pre><code>gcc -L/usr/local/lib -lmysqlclient myprog.o\n# undefined reference to `_mysql_init'\n# myprog depends on libmysqlclient\n# so myprog has to come earlier on the command line\n\ngcc myprog.o -L/usr/local/lib -lmysqlclient\n# undefined reference to `_uncompress'\n# we have to link with libz, too\n\ngcc myprog.o -lz -L/usr/local/lib -lmysqlclient\n# undefined reference to `_uncompress'\n# libz is needed by libmysqlclient\n# so it has to appear *after* it on the command line\n\ngcc myprog.o -L/usr/local/lib -lmysqlclient -lz\n# this works\n</code></pre>\n"}, {'answer_id': 6993142, 'author': 'yundorri', 'author_id': 885481, 'author_profile': 'https://Stackoverflow.com/users/885481', 'pm_score': 3, 'selected': False, 'text': "<p>You may can use -Xlinker option.</p>\n\n<pre><code>g++ -o foobar  -Xlinker -start-group  -Xlinker libA.a -Xlinker libB.a -Xlinker libC.a  -Xlinker -end-group \n</code></pre>\n\n<p>is ALMOST equal to</p>\n\n<pre><code>g++ -o foobar  -Xlinker -start-group  -Xlinker libC.a -Xlinker libB.a -Xlinker libA.a  -Xlinker -end-group \n</code></pre>\n\n<p><em><strong>Careful !</em></strong></p>\n\n<ol>\n<li>The order within a group is important !\nHere's an example: a debug library has a debug routine, but the non-debug\nlibrary has a weak version of the same. You must put the debug library\nFIRST in the group or you will resolve to the non-debug version.</li>\n<li>You need to precede each library in the group list with -Xlinker</li>\n</ol>\n"}, {'answer_id': 18172800, 'author': 'M.M', 'author_id': 1505939, 'author_profile': 'https://Stackoverflow.com/users/1505939', 'pm_score': 3, 'selected': False, 'text': '<p>A quick tip that tripped me up: if you\'re invoking the linker as "gcc" or "g++", then using "--start-group" and "--end-group" won\'t pass those options through to the linker -- nor will it flag an error. It will just fail the link with undefined symbols if you had the library order wrong.</p>\n\n<p>You need to write them as "-Wl,--start-group" etc. to tell GCC to pass the argument through to the linker.</p>\n'}, {'answer_id': 22555704, 'author': 'eckes', 'author_id': 520162, 'author_profile': 'https://Stackoverflow.com/users/520162', 'pm_score': 4, 'selected': False, 'text': "<p>Another alternative would be to specify the list of libraries twice:</p>\n\n<pre><code>gcc prog.o libA.a libB.a libA.a libB.a -o prog.x\n</code></pre>\n\n<p>Doing this, you don't have to bother with the right sequence since the reference will be resolved in the second block.</p>\n"}, {'answer_id': 29457226, 'author': 'SvaLopLop', 'author_id': 4152815, 'author_profile': 'https://Stackoverflow.com/users/4152815', 'pm_score': 6, 'selected': False, 'text': "<p>If you add <code>-Wl,--start-group</code> to the linker flags it does not care which order they're in or if there are circular dependencies.</p>\n\n<p>On Qt this means adding: </p>\n\n<pre><code>QMAKE_LFLAGS += -Wl,--start-group\n</code></pre>\n\n<p>Saves loads of time messing about and it doesn't seem to slow down linking much (which takes far less time than compilation anyway).</p>\n"}]
['https://Stackoverflow.com/questions/45135', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1597/']