Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you link correctly in C to stop symbols being stripped?
    text
    copied!<p>I'm having some trouble linking properly against libraries in C.</p> <p>I'm sure this is one of those arcane C linking rules I don't fully understand, but I can't figure it out.</p> <p>I have libn, which I compile into a static libary, libn.a</p> <p>nm libn shows:</p> <pre><code>doug@ninja:~/projects/libnw/build$ nm ../../libn/build/libn.a |grep nIndex 00000034 T nIndex 00000000 D nIndex_ 00000026 T nIndex_finalize 00000013 T nIndex_init 00000000 T nIndex_map </code></pre> <p>I also have libnw, which depends on libn. nm on libnw shows:</p> <pre><code>doug@ninja:~/projects/libnw/build$ nm libnw.a |grep Index U nIndex </code></pre> <p>However, when I compile a programming linking against libnw and libn I get:</p> <pre><code>doug@ninja:~/projects/libnw/build$ make [ 70%] Built target nw [ 80%] Built target test-template Scanning dependencies of target test-Core [ 85%] Building C object tests/nw/mvc/Core/CMakeFiles/test-Core.dir/Tests.c.o [ 90%] Building C object tests/nw/mvc/Core/CMakeFiles/test-Core.dir/test.c.o Linking C executable test-Core ../../../../libnw.a(Impl.c.o): In function `nwCore__Impl_init': /home/doug/projects/libnw/src/nw/mvc/Core/Impl.c:76: undefined reference to `nIndex' collect2: ld returned 1 exit status make[2]: *** [tests/nw/mvc/Core/test-Core] Error 1 make[1]: *** [tests/nw/mvc/Core/CMakeFiles/test-Core.dir/all] Error 2 make: *** [all] Error 2 </code></pre> <p>The reason, is quite clear. When Tests.c --> Tests.c.o, it's not picking nIndex up as a symbol it needs to keep:</p> <pre><code>doug@ninja:~/projects/libnw/build$ nm tests/nw/mvc/Core/CMakeFiles/test-Core.dir/Tests.c.o U MyController 000005a4 T Tests 00000000 D Tests_ 00000125 T Tests_can_attach_controller 00000080 T Tests_can_create_core 000003d3 T Tests_can_handle_native_event 000001c8 T Tests_can_set_controller 00000322 T Tests_can_update 00000000 t Tests_core_factory 0000056c T Tests_finalize 000005c0 T Tests_getType 0000048c T Tests_init U nFactory U nTest U nType_nalloc U nType_nfree U nwCore U nwDummyContext_getType U nwDummyEvents_getType U nwDummyRender_getType U nwIContext_getType U nwIEvents_getType U nwIRender_getType </code></pre> <p>(Notice the total absence of U nIndex in the tests object file).</p> <p>So, I can fix this easily by adding a call to nIndex() in my tests script, but that doesn't solve the basic problem:</p> <p>Program depends on liba depends on libb, liba has missing symbols from libb it needs to resolve, but program has no references to those symbols, so they seem to be being stripped.</p> <p>What am I doing wrong?</p> <p>(Yes, I'm building using cmake and depending on the statically built versions of libn and libnw).</p> <p>Edit:</p> <p>Now with linker line:</p> <pre><code>/usr/bin/gcc -std=c99 -g CMakeFiles/test-Core.dir/Tests.c.o \ CMakeFiles/test-Core.dir/test.c.o \ CMakeFiles/test-Core.dir/helpers/MyController.c.o \ CMakeFiles/test-Core.dir/helpers/MyModel.c.o \ -o test-Core -rdynamic \ /home/doug/projects/tapspin-android/source/deps/libn/build/libn.a \ ../../../../libnw.a </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload