将 Objective-C 应用程序链接到 C++静态库
我正在尝试使用 Xcode 构建一个链接到静态 C++ 库的 Objective-C 桌面应用程序。我正在使用苹果的 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)
它似乎没有链接到 C++ std 库。这是问题吗?如果是,我如何确保它链接到它?
更新:
如果我向构建添加一个空白的 cpp 文件,那么一切都会编译并运行良好。我似乎只是需要一种方法来告诉链接器链接到 C++ std 库。我尝试查看 Xcode 中的所有构建设置,但似乎找不到任何有帮助的内容。
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:
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试添加 -lstdc++ 或 -lc++ (取决于静态库所需的 C++ 运行时)
到“构建设置”下的“其他链接器标志”:
Try adding -lstdc++ or -lc++ (depending on which C++ runtime your static library expects)
to "Other Linker Flags" under "Build Setting":
与 Xcode 6 遇到同样的问题,我通过在“链接二进制文件与库”构建阶段和链接中链接“libstdc++.6.0.9.dylib”解决了这个问题。将 -lstdc++ 添加到“构建设置”下的“其他链接器标志”
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
看起来很奇怪,
libcore.a
正在std::ios_base
命名空间中寻找名为Init
的类。我不认为std::ios_base::Init
是标准类。除此之外,您可能需要检查
libcore.a
是否实际包含x86_64
代码。可以使用file
命令来检查。例如:如果
file
命令输出不包含“for Architecture x86_64
”行,则无法使用该库创建 64 位可执行文件。It seems weird that
libcore.a
is looking for a class namedInit
in thestd::ios_base
namespace. I don't thinkstd::ios_base::Init
is a standard class.Aside from that, you might want to check that
libcore.a
actually includesx86_64
code. You can use thefile
command to check. For example: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.其他链接器标志 -lstdc++ 在 Xcode5 中对我没有帮助。
但在 Apple LLVM 5.0 - Lnaguage - C++ 中发现类似的东西,
默认设置是 libc++(LLVM C++ standard~blabla) 并将其更改为 libstdc++(GNU thing)
现在链接没有错误了!!!现在我必须找出它运行良好。
不管怎样,谢谢你的建议。
Other Linker Flags -lstdc++ Didn't help for me in Xcode5.
But find out similar thing in Apple LLVM 5.0 - Lnaguage - C++
default setting was libc++(LLVM C++ standard~blabla) and changed it to libstdc++(GNU thing)
Now there is no error with linking!!! and now I have to find out it runs fine.
anyway thanks for advice.