10

I found examples and posts to

  • Create a static C++ lib for use in a C++ application
  • Create a static Objective C lib for use in an Objective C app.

What I couldn't find and have hassled around with for days now, is the correct way to create a static C++ lib for use in a Objective-C app under XCode 4.

I just want to use this very simple code for testing purposes:

#include <iostream>
#include "myCppLib.h"

using namespace std;

extern "C" void show_the_world() {
    cout << "Hello, world!\n";
}

I compile this with armv6/armv7 target, GCC 4.2 compiler, Linking 'C++ Standard Library Type' as 'Static' and have "Symbols Hidden by Default" to YES, as described by the Xcode help for static C++ libs.

My Objective-C app which calls the 'show_the_world' function errors about the std++ lib, which seems not to be included or not correctly referenced:

Undefined symbols for architecture armv6:
 "std::ios_base::Init::~Init()", referenced from:
  ___tcf_0 in libmyCppLib_dev.a(myCppLib.o)
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _show_the_world in libmyCppLib_dev.a(myCppLib.o)
  "std::ios_base::Init::Init()", referenced from:
  __static_initialization_and_destruction_0(int, int)in libmyCppLib_dev.a(myCppLib.o)
  "std::cout", referenced from:
  _show_the_world in libmyCppLib_dev.a(myCppLib.o)
ld: symbol(s) not found for architecture armv6
collect2: ld returned 1 exit status

I am sure that I compiled the library for the right architecture, so there must be something wrong in another setting or with my code.

Any help is welcome!

Best regards, jimmy

Perception
  • 79,279
  • 19
  • 185
  • 195
Jimmy Koerting
  • 1,231
  • 1
  • 14
  • 27
  • This thread may help - http://stackoverflow.com/questions/376966/using-c-c-static-libraries-from-iphone-objectivec-apps. – Perception Jul 30 '11 at 12:13
  • 5
    I already new this post, but because of my 'not so good' english, I did not found the answer in this post. After you redirected me back to this, I took myself a little more time - and now it works :) the point was to include the std++ lib into the compiling of the resulting objective-c project, too. So, thanks a lot! – Jimmy Koerting Aug 09 '11 at 13:38
  • yeah, libstdc++.dylib solves the problem, amazing! – pronebird Apr 08 '12 at 13:05

2 Answers2

10

The correct answer was provided by Jimmy Koerting in the comments: the app needs to be linked against libstdc++.dylib to resolve the standard library symbols. Thanks to Jimmy Koerting, but one more thing i want to add here is, if you are using xcode latest version with iOS 6.1 please add this libstdc++.6.dylib

user2134566
  • 101
  • 1
  • 2
  • Yes, on my Xcode 5.0.2 I had to use libstdc++.6.dylib to resolve the linker errors. Using libstdc++.dylib or libc++.dylib did not help. – Randall Cook Jan 07 '14 at 00:19
2

Answered in my own comment ;)

the point was to include the std++ lib into the compiling of the resulting objective-c project, too.

Jimmy Koerting
  • 1,231
  • 1
  • 14
  • 27