交叉编译 C++/Objective-C++ 时出错

发布于 2025-01-04 21:23:56 字数 4317 浏览 2 评论 0原文

我有一个 Objective-C++ 项目引用的 C++ 库。该库可以自行编译,并且 Objective-C++ 项目也可以很好地编译,直到我实例化库中的第一个类(使用对象指针)。在使用我的库中的类之前,我引用了不会引起问题的 stl string 对象。

我收到以下错误:

Undefined symbols for architecture i386:
  "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:
      __verify_callback_c in *************.a(cxx_db.o)
      DbEnv::_stream_message_function(__db_env const*, char const*) in *************.a(cxx_env.o)
      DbEnv::_stream_error_function(__db_env const*, char const*, char const*) in *************.a(cxx_env.o)
  "std::basic_ios<char, std::char_traits<char> >::fail() const", referenced from:
      __verify_callback_c in *************.a(cxx_db.o)
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_db.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_dbc.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_dbt.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_env.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_mpool.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_txn.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_lock.o)
      ...
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in *************.a(cxx_db.o)
      ___tcf_0 in *************.a(cxx_dbc.o)
      ___tcf_0 in *************.a(cxx_dbt.o)
      ___tcf_0 in *************.a(cxx_env.o)
      ___tcf_0 in *************.a(cxx_mpool.o)
      ___tcf_0 in *************.a(cxx_txn.o)
      ___tcf_0 in *************.a(cxx_lock.o)
      ...
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

到目前为止,我发现此类错误可能是由以下原因引起的:

  1. 文件类型为 .m 而不是 .mm
  2. 在我的 Objective-C++ 中创建 C++ 对象而不使用

指针假设这是某种交叉编译错误,但我不知道去哪里查找。关于它可能是什么的想法?

编辑:

Build Settings:
C++ Standard Library = libc++
C++ Language Dialect = c++0x

这些在我的顶级项目中。包含的库也使用这些(它也引用外部库,并且将其添加到我的主项目时不再找到标头路径 - 我必须将其添加到我的主项目设置)。

编辑2: 以下是失败的构建步骤:

Ld /Users/user/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Products/Debug-iphonesimulator/BerkeleyDBHelloWorldSimulator.app/BerkeleyDBHelloWorldSimulator 正常 i386 cd /Users/用户/Documents/Projects/Tests/BerkeleyDBHelloWorldSimulator setenv MACOSX_DEPLOYMENT_TARGET 10.6 setenv PATH“/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin” /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/clang++ -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk -L/Users/用户/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Products/Debug-iphonesimulator -F/Users/用户/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Products/Debug-iphonesimulator -filelist /Users/用户/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Intermediates/BerkeleyDB HelloWorldSimulator.build/Debug-iphonesimulator/BerkeleyDBHelloWorldSimulator.build/Objects-normal/i386/BerkeleyDBHelloWorldSimulator.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -stdlib=libc++ -Xlinker -no_implicit_dylibs -D__IPHONE_OS_VERSION_MIN_REQUIRED=50000 -lstdc++ /Users/用户/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Products/Debug-iphonesimulator/libBerkeleyDB.a -framework UIKit -framework Foundation -framework CoreGraphics -o /Users/用户/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Products/Debug-iphonesimulator/BerkeleyDBHelloWorldSimulator.app/BerkeleyDBHelloWorldSimulator

I have a C++ library being referenced by an Objective-C++ project. The library compiles fine on its own and the Objective-C++ project compiles fine until I instantiate the first class from the library (using an object pointer). Before using the class from my library I had references to stl string objects that didn't cause problems.

I'm getting the following errors:

Undefined symbols for architecture i386:
  "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:
      __verify_callback_c in *************.a(cxx_db.o)
      DbEnv::_stream_message_function(__db_env const*, char const*) in *************.a(cxx_env.o)
      DbEnv::_stream_error_function(__db_env const*, char const*, char const*) in *************.a(cxx_env.o)
  "std::basic_ios<char, std::char_traits<char> >::fail() const", referenced from:
      __verify_callback_c in *************.a(cxx_db.o)
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_db.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_dbc.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_dbt.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_env.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_mpool.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_txn.o)
      __static_initialization_and_destruction_0(int, int) in *************.a(cxx_lock.o)
      ...
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in *************.a(cxx_db.o)
      ___tcf_0 in *************.a(cxx_dbc.o)
      ___tcf_0 in *************.a(cxx_dbt.o)
      ___tcf_0 in *************.a(cxx_env.o)
      ___tcf_0 in *************.a(cxx_mpool.o)
      ___tcf_0 in *************.a(cxx_txn.o)
      ___tcf_0 in *************.a(cxx_lock.o)
      ...
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

So far I've found that errors like these can be caused by:

  1. Having a file type of .m instead of .mm
  2. Creating a C++ object in my Objective-C++ without using a pointer

I'm assuming it's some sort of cross-compile error, but I don't know where to look. Ideas on what it could be?

EDIT:

Build Settings:
C++ Standard Library = libc++
C++ Language Dialect = c++0x

These are in my top-level project. The contained library is using these as well (it references an outside library as well, and the header path was no longer found when added it to my main project - I had to add it to my main project settings).

Edit 2:
Here is the build step that fails:

Ld /Users/user/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Products/Debug-iphonesimulator/BerkeleyDBHelloWorldSimulator.app/BerkeleyDBHelloWorldSimulator normal i386
cd /Users/user/Documents/Projects/Tests/BerkeleyDBHelloWorldSimulator
setenv MACOSX_DEPLOYMENT_TARGET 10.6
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/clang++ -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk -L/Users/user/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Products/Debug-iphonesimulator -F/Users/user/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Products/Debug-iphonesimulator -filelist /Users/user/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Intermediates/BerkeleyDBHelloWorldSimulator.build/Debug-iphonesimulator/BerkeleyDBHelloWorldSimulator.build/Objects-normal/i386/BerkeleyDBHelloWorldSimulator.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -stdlib=libc++ -Xlinker -no_implicit_dylibs -D__IPHONE_OS_VERSION_MIN_REQUIRED=50000 -lstdc++ /Users/user/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Products/Debug-iphonesimulator/libBerkeleyDB.a -framework UIKit -framework Foundation -framework CoreGraphics -o /Users/user/Library/Developer/Xcode/DerivedData/BerkeleyDBHelloWorldSimulator-bgnkrqnronvtkoaongfsdturoklb/Build/Products/Debug-iphonesimulator/BerkeleyDBHelloWorldSimulator.app/BerkeleyDBHelloWorldSimulator

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

痴梦一场 2025-01-11 21:23:56

我们花了一些时间才在评论线程中找到接近解决方案的内容,因此我将在这里总结一下:

这种类型的错误是由于未成功链接到正确的 libstdc++.dylib 引起的,标准 C++ 库。有时,您忘记添加它,有时 XCode 会感到困惑。

首先,确保您确实在应用程序目标的设置中链接到它:它必须出现在“构建阶段”、“将二进制文件与库链接”中。

如果它在那里,但仍然无法链接,请检查原始构建日志中是否存在 XCode 可能向您隐藏的任何警告:例如有关错误架构的警告。这意味着您链接到错误的文件版本 - 例如,链接到模拟器或 OSX 版本,而不是 iPhoneOSx.y.sdk 目录中的版本。过去,在 XCode 3 中,这确实很容易出错,解决这个问题的唯一方法是尝试 XCode 提供的每一个不同的 libstdc++.dylib ,直到有一个可以工作为止。除了带有版本号的名称之外,XCode4 通常只提供一个。您通常应该选择没有版本号的 dylib。

确保所有可能链接到 libstdc++.dylib 的子项目在同一位置使用相同版本。

检查“库搜索路径”和“框架搜索路径”中是否有任何不是您自己添加的路径。我在这里让 XCode 设置了旧 SDK 的路径,这使链接器感到困惑。

最后,有时删除对 libstdc++.dylib 的引用、清理项目、退出并重新启动 XCode,然后重新添加引用会有所帮助。

It's taken us some time to get to something approaching a solution in the comments thread, so I'm going to summarise it here:

This type of error is caused by not successfully linking to the correct libstdc++.dylib, the standard C++ library. Sometimes, you forget to add it, sometimes XCode gets confused.

First of all, make sure you are indeed linking to it in your app target's settings: it must appear in "build phases", "link binary with libraries".

If it's there, but still failing to link, check the raw build log for any warnings that XCode might be hiding from you: e.g. about wrong architecture. This means you're linking to the wrong version of the file - e.g. linking against the simulator or OSX version instead of that in the iPhoneOSx.y.sdk directory. In the past, with XCode 3, this was really easy to get wrong, and the only way to fix it was to try every single different libstdc++.dylib that XCode offered until one worked. XCode4 normally only offers one, in addition to the names with version numbers. You should normally pick the dylib without a version number.

Make sure all subprojects that may link against libstdc++.dylib use the same version in the same location.

Check the "Library Search Paths" and "Framework Search Paths" for any paths that you didn't add yourself. I've had XCode set paths to old SDKs here, which confused the linker.

Finally, sometimes it helps to just remove the reference to libstdc++.dylib, clean the project, quit and restart XCode, and re-add the reference.

季末如歌 2025-01-11 21:23:56

确保您的代码和库都链接到同一个 C++ 标准库。

Make sure both your code and the library links against the same C++ standard library.

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