Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link Objective-C application to C++ static library

I am trying to use Xcode to build an Objective-C desktop application that links against a static C++ library. I am using the Apple's clang compiler. I am getting the following linker error:

Apple clang version 3.0 (tags/Apple/clang-211.12) (based on LLVM 3.0svn)
Target: x86_64-apple-darwin11.2.0
Thread model: posix
 "/Developer/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.7.0 -syslibroot /Developer/SDKs/MacOSX10.7.sdk -o /Users/andrew/Library/Developer/Xcode/DerivedData/sl-marketplace-analysis-exomzzahbygseghhwoeclcvpooeo/Build/Products/Debug/sl marketplace analyitics.app/Contents/MacOS/sl marketplace analyitics -lcrt1.10.6.o -L/Users/andrew/Library/Developer/Xcode/DerivedData/sl-marketplace-analysis-exomzzahbygseghhwoeclcvpooeo/Build/Products/Debug -L/Users/andrew/Projects/sl-marketplace-analysis/platform/mac/sl marketplace analyitics/../../../../../Library/Developer/Xcode/DerivedData/sl-marketplace-analysis-exomzzahbygseghhwoeclcvpooeo/Build/Products/Debug -filelist /Users/andrew/Library/Developer/Xcode/DerivedData/sl-marketplace-analysis-exomzzahbygseghhwoeclcvpooeo/Build/Intermediates/sl marketplace analyitics.build/Debug/sl marketplace analyitics.build/Objects-normal/x86_64/sl marketplace analyitics.LinkFileList -framework Cocoa -lcore -lSystem /Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a -F/Users/andrew/Library/Developer/Xcode/DerivedData/sl-marketplace-analysis-exomzzahbygseghhwoeclcvpooeo/Build/Products/Debug
Undefined symbols for architecture x86_64:
  "std::ios_base::Init::~Init()", referenced from:
      ___cxx_global_var_init in libcore.a(test.o)
  "std::ios_base::Init::Init()", referenced from:
      ___cxx_global_var_init in libcore.a(test.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It seems like it isn't linking against the C++ std library. Is this the problem and if so, how to I ensure that it links against it?

Update:

If I add a blank cpp file to the build then everything compiles and runs fine. I just seem to need a way to tell the linker to link against the C++ std library. I have tried looking through all of the build settings in Xcode but can't seem to find anything that helps.

like image 958
drewag Avatar asked Jan 21 '12 03:01

drewag


3 Answers

Try adding -lstdc++ or -lc++ (depending on which C++ runtime your static library expects) to "Other Linker Flags" under "Build Setting":

enter image description here

like image 136
servn Avatar answered Nov 17 '22 05:11

servn


Having same issue with Xcode 6, i have solved it by linking "libstdc++.6.0.9.dylib" in the "Link Binary with Libraries" build phase & adding -lstdc++ to "Other Linker Flags" under Build Setting

like image 30
Urmi Avatar answered Nov 17 '22 05:11

Urmi


It seems weird that libcore.a is looking for a class named Init in the std::ios_base namespace. I don't think std::ios_base::Init is a standard class.

Aside from that, you might want to check that libcore.a actually includes x86_64 code. You can use the file command to check. For example:

$ file /usr/lib/libz.dylib
/usr/lib/libz.dylib: Mach-O universal binary with 2 architectures
/usr/lib/libz.dylib (for architecture x86_64):  Mach-O 64-bit dynamically linked shared library x86_64
/usr/lib/libz.dylib (for architecture i386):    Mach-O dynamically linked shared library i386

If the file command output doesn't include a “for architecture x86_64” line, you can't use the library to create a 64-bit executable.

like image 1
rob mayoff Avatar answered Nov 17 '22 05:11

rob mayoff