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

发布于 2024-12-28 07:53:56 字数 1968 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲念泪 2025-01-04 07:53:56

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

在此处输入图像描述

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

enter image description here

指尖凝香 2025-01-04 07:53:56

与 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

策马西风 2025-01-04 07:53:56

看起来很奇怪,libcore.a 正在 std::ios_base 命名空间中寻找名为 Init 的类。我不认为 std::ios_base::Init 是标准类。

除此之外,您可能需要检查 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

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

It seems weird that libcore.a is looking for a class named Init in the std::ios_base namespace. I don't think std::ios_base::Init is a standard class.

Aside from that, you might want to check that libcore.a actually includes x86_64 code. You can use the file command to check. 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.

铁轨上的流浪者 2025-01-04 07:53:56

其他链接器标志 -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文