简体   繁体   English

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

[英]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. 我正在尝试使用Xcode构建链接到静态C ++库的Objective-C桌面应用程序。 I am using the Apple's clang compiler. 我正在使用Apple的clang编译器。 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. 似乎它没有链接到C ++ std库。 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. 如果我将空白cpp文件添加到构建中,则所有内容都会编译并运行良好。 I just seem to need a way to tell the linker to link against the C++ std library. 我似乎只需要一种方法来告诉链接器链接到C ++ std库。 I have tried looking through all of the build settings in Xcode but can't seem to find anything that helps. 我尝试查看Xcode中的所有构建设置,但似乎找不到任何有用的东西。

Try adding -lstdc++ or -lc++ (depending on which C++ runtime your static library expects) to "Other Linker Flags" under "Build Setting": 尝试将-lstdc ++或-lc ++(取决于您的静态库期望的C ++运行时)添加到“构建设置”下的“其他链接器标志”中:

在此处输入图片说明

在Xcode 6中也有同样的问题,我通过在“ 链接二进制文件与库 ”构建阶段中链接“ libstdc ++。6.0.9.dylib ”并在构建设置下将-lstdc ++添加到“ 其他链接器标志 ”中来解决了

It seems weird that libcore.a is looking for a class named Init in the std::ios_base namespace. libcore.astd::ios_base命名空间中寻找名为Init的类似乎很奇怪。 I don't think std::ios_base::Init is a standard class. 我不认为std::ios_base::Init是标准类。

Aside from that, you might want to check that libcore.a actually includes x86_64 code. 除此之外,您可能想要检查libcore.a确实包含x86_64代码。 You can use the file command to check. 您可以使用file命令进行检查。 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. 如果file命令输出中不包含“ for architecture x86_64 ”行,则无法使用该库创建64位可执行文件。

Other Linker Flags -lstdc++ Didn't help for me in Xcode5. 其他链接器标志-lstdc ++在Xcode5中对我没有帮助。 But find out similar thing in Apple LLVM 5.0 - Lnaguage - C++ 但是在Apple LLVM 5.0中发现了类似的情况-语言-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. 无论如何,谢谢您的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM