title: Linker error using gcc and clang together on macos sierra
body: <p>I have to compile c++ code with <em>g++ 6.4.0</em> (Homebrew g++-6) to a static lib, which is then wrapped into a C static lib (Homebrew gcc-6) and linked to a <em>clang++</em> (clang 8.1.0) app on <em>macos sierra</em>. So the picture is:</p>
  
  <pre><code>c++ (gcc) wrapped in c (gcc) linked to clang app.
  </code></pre>
  
  <p>As a testcase I use <em>shared-lib.cpp</em>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  void foo()
  {
    cerr &lt;&lt; "Hi from the shared lib" &lt;&lt; endl;
  } 
  </code></pre>
  
  <p>together with <em>shared-lib.h</em></p>
  
  <pre><code>extern void foo(); 
  </code></pre>
  
  <p>and <em>wrapper-lib.c</em></p>
  
  <pre><code>#include "shared-lib.h"
  int wrapper()  
  {
    foo();
    return 123;
  } 
  </code></pre>
  
  <p>along with <em>wrapper-lib.h</em></p>
  
  <pre><code>#ifdef __cplusplus
  extern "C"
  {
  #endif
  
  extern int wrapper();
  
  #ifdef __cplusplus
  }
  #endif 
  </code></pre>
  
  <p>The <em>main.cpp</em> that uses all the libs looks like</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  #include "shared-lib.h"
  #include "wrapper-lib.h"
  using namespace std;
  
  int main()
  {
      auto s = "Hello world from main";
      cout &lt;&lt; s &lt;&lt; endl;
      foo(); // from c++ lib
  
      int result = wrapper();  // from c wrapper lib
      cout &lt;&lt; "wrapper returned " &lt;&lt; result &lt;&lt; endl;
  
      return 0;
  } 
  </code></pre>
  
  <p>My test built script is</p>
  
  <pre><code>g++-6 --version
  echo -----------------------
  
  echo build shared-lib .o with g++
  g++-6 -c -Wall -fpic -std=c++11 shared-lib.cpp
  
  echo build a wrapper library in C with gcc
  gcc-6 -c -Wall -fpic  wrapper-lib.c
  
  echo build static libshared-lib.a
  ar rcs libshared-lib.a  shared-lib.o
  
  echo build static libwrapper-lib.a
  ar rcs libwrapper-lib.a  wrapper-lib.o
  
  echo build main with clang
  clang++ --version
  echo ----------------------
  clang++ -v -L/Users/worker -Wall -std=c++11 -stdlib=libstdc++ -lwrapper-lib  -lshared-lib main.cpp -o main
  
  echo start the app
  ./main 
  </code></pre>
  
  <p>If I only call the gcc c++ function <em>foo()</em> then everything works fine.
  If I call the C wrapper function <em>wrapper()</em>, then clang comes up with:</p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "_foo", referenced from:
        _wrapper in libwrapper-lib.a(wrapper-lib.o)
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>Maybe someone can simply spot, what's wrong with my workflow?</p>
  
  <p><strong>Note</strong>, for completeness the whole build script output</p>
  
  <p><strong>Note2</strong>, since <em>ar</em> in the gcc@6 toolchain does not work (liblto_plugin.so missing) I use clang's <em>ar</em> tool...</p>
  
  <pre><code>mac-mini:~ worker$ ./build-test.sh
  g++-6 (Homebrew GCC 6.4.0) 6.4.0
  Copyright (C) 2017 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  
  -----------------------
  build shared-lib .o with g++
  build a wrapper library in C with gcc
  build static libshared-lib.a
  build static libwrapper-lib.a
  build main with clang
  Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
  Apple LLVM version 8.1.0 (clang-802.0.41)
  Target: x86_64-apple-darwin16.7.0
  Thread model: posix
  InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
  ----------------------
  Apple LLVM version 8.1.0 (clang-802.0.41)
  Target: x86_64-apple-darwin16.7.0
  Thread model: posix
  InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
  clang: warning: libstdc++ is deprecated; move to libc++ [-Wdeprecated]
   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 278.4 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.1.0 -stdlib=libstdc++ -Wall -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/worker -ferror-limit 19 -fmessage-length 166 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/18/m18t0kxx03d7__31kg3wrsr40000gq/T/main-337db7.o -x c++ main.cpp
  clang -cc1 version 8.1.0 (clang-802.0.41) default target x86_64-apple-darwin16.7.0
  ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin10/x86_64"
  ignoring nonexistent directory "/usr/include/c++/4.0.0"
  ignoring nonexistent directory "/usr/include/c++/4.0.0/i686-apple-darwin8/"
  ignoring nonexistent directory "/usr/include/c++/4.0.0/backward"
  #include "..." search starts here:
  #include &lt;...&gt; search starts here:
   /usr/include/c++/4.2.1
   /usr/include/c++/4.2.1/backward
   /usr/local/include
   /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.1.0/include
   /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
   /usr/include
   /System/Library/Frameworks (framework directory)
   /Library/Frameworks (framework directory)
  End of search list.
   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o main -L/Users/worker -lwrapper-lib -lshared-lib /var/folders/18/m18t0kxx03d7__31kg3wrsr40000gq/T/main-337db7.o -lstdc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.1.0/lib/darwin/libclang_rt.osx.a
  Undefined symbols for architecture x86_64:
    "_foo", referenced from:
        _wrapper in libwrapper-lib.a(wrapper-lib.o)
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>