xcode 将 Objective-C 应用程序链接到 C++ 静态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8950388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:03:15  来源:igfitidea点击:

Link Objective-C application to C++ static library

c++xcodelinkerstatic-librariesclang

提问by drewag

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:

我正在尝试使用 Xcode 来构建一个链接到静态 C++ 库的 Objective-C 桌面应用程序。我正在使用 Apple 的 clang 编译器。我收到以下链接器错误:

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?

似乎它没有链接到 C++ std 库。这是问题吗?如果是,我如何确保它链接到它?

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.

如果我将一个空白的 cpp 文件添加到构建中,那么一切都会编译并运行良好。我似乎只是需要一种方法来告诉链接器链接到 C++ std 库。我尝试查看 Xcode 中的所有构建设置,但似乎找不到任何有用的东西。

回答by servn

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

尝试将 -lstdc++ 或 -lc++(取决于静态库期望的 C++ 运行时)添加到“构建设置”下的“其他链接器标志”:

enter image description here

在此处输入图片说明

回答by Urmi

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

与 Xcode 6 有同样的问题,我通过在“ Link Binary with Libraries”构建阶段链接“ libstdc++.6.0.9.dylib”并将-lstdc++添加到“其他链接器标志”在构建设置下解决了这个问题

回答by rob mayoff

It seems weird that libcore.ais looking for a class named Initin the std::ios_basenamespace. I don't think std::ios_base::Initis a standard class.

这似乎不可思议的是libcore.a寻找一个名为类Initstd::ios_base命名空间。我不认为std::ios_base::Init是标准班。

Aside from that, you might want to check that libcore.aactually includes x86_64code. You can use the filecommand to check. For example:

除此之外,您可能需要检查是否libcore.a实际包含x86_64代码。您可以使用该file命令进行检查。例如:

$ 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 filecommand output doesn't include a “for architecture x86_64” line, you can't use the library to create a 64-bit executable.

如果file命令输出不包含“ for architecture x86_64”行,则不能使用该库创建 64 位可执行文件。

回答by user2958279

Other Linker Flags -lstdc++Didn't help for me in Xcode5. But find out similar thing in Apple LLVM 5.0 - Lnaguage - C++

其他链接器标志 -lstdc++在 Xcode5 中对我没有帮助。但是在Apple LLVM 5.0 - Lnaguage - C++ 中找出类似的东西

default setting was libc++(LLVM C++ standard~blabla)and changed it to libstdc++(GNU thing)

默认设置是libc++(LLVM C++ 标准~blabla)并将其更改为libstdc++(GNU 东西)

Now there is no error with linking!!! and now I have to find out it runs fine.

现在链接没有错误!!!现在我必须发现它运行良好。

anyway thanks for advice.

无论如何感谢您的建议。