构建静态C ++库以与Xcode 4中的Objective-c应用程序一起使用

时间:2022-06-28 05:42:19

I found examples and posts to

我找到了示例和帖子

  • Create a static C++ lib for use in a C++ application
  • 创建一个静态C ++库,以便在C ++应用程序中使用

  • Create a static Objective C lib for use in an Objective C app.
  • 创建一个静态Objective C lib,以便在Objective C应用程序中使用。

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.

我现在找不到并且已经困扰了几天,是创建静态C ++库以在XCode 4下的Objective-C应用程序中使用的正确方法。

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.

我使用armv6 / armv7目标,GCC 4.2编译器,将'C ++标准库类型'链接为'静态'并将“默认隐藏的符号”编译为YES,如静态C ++库的Xcode帮助所述。

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:

我的Objective-C应用程序调用了关于std ++ lib的'show_the_world'函数错误,它似乎没有包含或未正确引用:

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

最好的问候,吉米

2 个解决方案

#1


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

Jimmy Koerting在评论中提供了正确答案:应用程序需要与libstdc ++。dylib链接以解析标准库符号。感谢Jimmy Koerting,但我还想补充一点,如果你在iOS 6.1上使用xcode最新版本,请添加这个libstdc ++。6.dylib

#2


2  

Answered in my own comment ;)

在我自己的评论中回答;)

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

关键是要将std ++ lib包含在最终的objective-c项目的编译中。

#1


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

Jimmy Koerting在评论中提供了正确答案:应用程序需要与libstdc ++。dylib链接以解析标准库符号。感谢Jimmy Koerting,但我还想补充一点,如果你在iOS 6.1上使用xcode最新版本,请添加这个libstdc ++。6.dylib

#2


2  

Answered in my own comment ;)

在我自己的评论中回答;)

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

关键是要将std ++ lib包含在最终的objective-c项目的编译中。