28

I need help. I had a framwork which was using stdc++ like std:string. Now when i have created new app for IOS7 only there is problem with linking this framework because of problems with stdc++ lib:

Undefined symbols for architecture armv7 "std::basic_string, std::allocator >::_Rep::_S_empty_rep_storage", referenced from ...

I have find out something strange that when i change the Deplyment target to ios6 in this app all is working fine. With ios7 i see errors.

I already have flag set in other linker flags: -lstdc++

Any idea what ami doing wrong?

MarekM
  • 568
  • 1
  • 5
  • 11
  • are any of the files in your project using the Objective-C++ file extension of "`.mm`" and if not, what happens when you change one of them to have that file extension? – Michael Dautermann Sep 23 '13 at 12:54
  • i use .mm in one of the framework classes which ii link. When i have changed one of the file in normal project nothing has changed and problem is still present. – MarekM Sep 23 '13 at 13:57
  • what's the c++ dialect set to? – CodeSmile Sep 23 '13 at 14:05

4 Answers4

69

To be honest, I don't like the above answer, as it uses the static lib and not the dynamic!

I have had the problem myself and found that the problem is that xcode is unable to find any C++ files in you project and thus assumes that the libstc++ is not needed!

Quite annoying when you are linking to a static lib that uses it!!

Solution :

  • Add a empty .mm file to you project!
  • Ensure that C++ Standard Library is set to libstdc++ (GNU c++ standard library) in the Build Settings

This worked for me and I did not have to add -lstdc++ to Other Linker Flags!

starball
  • 20,030
  • 7
  • 43
  • 238
rimestad
  • 728
  • 1
  • 4
  • 6
  • Thanks @rimestad, that worked! Do you also know why we have to go with libstdc++ instead of libc++? The default is libc++ but it will still not work with libc++. – Guven Oct 09 '13 at 15:40
  • 2
    This should be the accepted answer. Also, you actually don't have to set the C++ Standard Library to libstdc++ anymore after adding an empty cpp or mm file. – Lewion Jan 06 '15 at 10:24
  • 5
    Add a empty .mm file to you project! rocks – Cullen SUN Jan 07 '15 at 17:29
  • @rimestad i had similar linker error(library not found for -lstdc++).i tried everything but no use.any idea? – Dharma Sep 01 '15 at 13:03
  • A complete life saver. Worked for when i was supporting an app for iOS 11. Thanks a lot mate :) – Ankit Kumar Gupta Aug 13 '17 at 20:12
27

Thanks for your help. I have found solution. I was using -lstdc++ flag in other linker flags but it is not enough now. I had to add "libstdc++.6.0.9.dylib in BuildPhases->Link Binary With Libraries. There was somewhere in the net info that xcode has problems with ios7 stdc++ lib selection and it should be selected manually.

Regards, Marek

MarekM
  • 568
  • 1
  • 5
  • 11
27

Just an update on this answer:

This step is very important!

Ensure that C++ Standard Library is set to libstdc++ (GNU c++ standard library) in the Apple LLVM 5.0 Compiler Build Settings

Background:

I have an iOS app that abruptly stopped building for iOS 7 with standard library link errors. I had been testing the app successfully on the simulator and on an iPad mini, and archived it as well, but when I added an iPhone 5S for testing, I started getting link errors (possibly because of a new architecture in the mix?).

Searching the link errors on Google, I found advice to explicitly add libstdc++.dylib to the linked libraries. This did not work.

I experimented by adding libstdc++.6.dylib to the linked libraries instead; this eliminated the link errors, but the app crashed in the standard C++ library code very early.

Removing the explicit additions of the library and changing the compiler setting in Build Settings, as noted above, corrected the link errors and runtime problem.

Thanks to rimestad for the pointer!

Doug Knowles
  • 873
  • 1
  • 8
  • 14
  • I have done this, but still get the standard library linker errors (I'm trying to link OpenCV). Any idea what else might be going wrong? It happens the same for iOS6 or 7 as deployment target. – fredley Jan 20 '14 at 15:07
  • Yes, clang and gcc have different stdlibs and if you link a library built with gcc you'll end up with this exact issue. – Cameron Lowell Palmer Sep 16 '14 at 13:02
3

I had a similar problem, but @MarekM's answer only solve part of my problem, so I tried to add libstdc++.dylib and libstdc++.6.dylib, and that makes all the compiling error gone.

wzhang84
  • 150
  • 1
  • 5