title: Wrapping symbols during linking on OS X
body: <p>I'm trying to wrap one symbol by another during link. As I understand this is easily done with ld --wrap option, but on OS X it's not available. There is '-idefinition:indirection', here how I'm trying to cheat main so it will print 42:</p>
  
  <p>a.cpp:</p>
  
  <pre><code>int foo() {
      return 1;
  }
  </code></pre>
  
  <p>b.cpp:  </p>
  
  <pre><code>int wrap_foo() {
      return 42;
  }
  </code></pre>
  
  <p>main.cpp:  </p>
  
  <pre><code>#include &lt;cstdio&gt;
  
  int foo();
  int wrap_foo();
  
  int main() {
      printf("%d\n", foo());
  }
  </code></pre>
  
  <p>How I build and link them:</p>
  
  <pre><code>$ gcc -c *.cpp
  $ gcc -Wl,-i__Z3foov:__Z8wrap_foov *.o
  duplicate symbol __Z3foov in:
      a.o
      b.o
  ld: 1 duplicate symbol for architecture x86_64
  </code></pre>
  
  <p>Is it even possible to achieve what I want?</p>
  
  <hr>
  
  <p>EDIT:
  In my task I don't have control over a.cpp and main.cpp (have only objects for them), but I can write any code and do whatever I need with the objects before link.</p>
  
  <p>I've found following quistion before which related to similar task (and describes it better)
  <a href="https://stackoverflow.com/questions/13961774/gnu-gcc-ld-wrapping-a-call-to-symbol-with-caller-and-callee-defined-in-the-sam">GNU gcc/ld - wrapping a call to symbol with caller and callee defined in the same object file</a>
  Rereading the answers I see that the case is same, symbol already present and I will have a collision.</p>
  
  <p><strong>How to remove foo from a.o without touching source code?</strong></p>