Note that there are some explanatory texts on larger screens.

plurals
  1. POIndirect linking trouble on OSX
    text
    copied!<p>I have some trouble with indirect linking. Here is a small example that shows what's going wrong:</p> <pre><code>$ make g++ -fPIC -Wall -c -o main.o main.cpp g++ -fPIC -Wall -c -o a.o a.cpp g++ -fPIC -Wall -c -o b.o b.cpp g++ -fPic -Wall -r -dynamiclib b.o -o libb.dylib g++ -fPic -Wall -r -dynamiclib a.o -o liba.dylib -L. -lb LD_LIBRARY_PATH=. g++ main.o -o runme -L. -la -O /usr/libexec/gcc/powerpc-apple-darwin8/4.0.1/ld: Undefined symbols: fooB() collect2: ld returned 1 exit status make: *** [runme] Error 1 </code></pre> <p>Makefile </p> <pre><code>all: runme CXXFLAGS=-fPIC -Wall runme: main.o liba.dylib libb.dylib LD_LIBRARY_PATH=. g++ main.o -o runme -L. -la -O libb.dylib: b.o g++ -fPic -Wall -r -dynamiclib b.o -o libb.dylib liba.dylib: a.o libb.dylib g++ -fPic -Wall -r -dynamiclib a.o -o liba.dylib -L. -lb clean: rm -f *.o *.dylib runme </code></pre> <p>a.h</p> <pre><code>#ifndef A_H_INCLUDED #define A_H_INCLUDED // liba depends on libb #include "b.h" // call through to fooB, implemented in the header. inline int fooA() { return fooB(); } // call through to barB, implemented in the library. int barA(); #endif // A_H_INCLUDED </code></pre> <p>a.cpp</p> <pre><code>#include "a.h" int barA() { return barB(); } </code></pre> <p>b.h</p> <pre><code>#ifndef B_H_INCLUDED #define B_H_INCLUDED int fooB(); int barB(); #endif // B_H_INCLUDED </code></pre> <p>b.cpp</p> <pre><code>#include "b.h" int fooB() { return 42; } int barB() { return 314; } </code></pre> <p>main.cpp</p> <pre><code>#include &lt;iostream&gt; #include "a.h" int main() { std::cout &lt;&lt; barA() &lt;&lt; "\n"; std::cout &lt;&lt; fooA() &lt;&lt; "\n"; return 0; } </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