【问题标题】:'ld: symbol(s) not found for architecture ???' even when it's there'ld: 未找到架构的符号 ???'即使它在那里
【发布时间】:2014-07-26 01:34:15
【问题描述】:

我在尝试在 C++ 中添加对 boost 模块的引用时遇到了这个奇怪的问题。首先,你需要知道我在玩一点 C++,所以也许这是我错过的一个超级简单的菜鸟。

这是我的环境,因此您可以获得更多背景信息:Mac OS Mavericks,带有 boost 1.55 和 g++-mp-4.8 的 MacPorts 端口。我目前正在链接和使用boost_system-mtboost_filesystem-mt

问题是我正在从事一个正在进行的项目,它一直在使用 boost 并且编译良好。我正在尝试将utf8 字符串转换为Latin1,因此我包含boost/locale.hpp,并将-lboost_locale-mt 添加到链接器参数中。对于我正在做的任务:

boost::locale::conv::from_utf<char>(value, "Latin1")

到目前为止一切顺利,编译正常,但是当链接应该完成时,我收到以下错误:

Undefined symbols for architecture x86_64:
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> > boost::locale::conv::from_utf<char>(char const*, char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::locale::conv::method_type)", referenced from:
      clsByteQueue::WriteUnicodeStringFixed(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in clsByteQueue.o
      clsByteQueue::WriteUnicodeString(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in clsByteQueue.o
ld: symbol(s) not found for architecture x86_64

链接器正在使用以下命令运行:

g++-mp-4.8 -fstack-protector -ggdb -o "bin/foobar" <all compiled.o files goes here> -L/opt/local/lib/ -levent -levent_core -levent_extra -lboost_system-mt -lboost_filesystem-mt -lboost_locale-mt

问题是,我检查了/opt/local/lib/libboost_locale-mt.a 不仅存在,而且还针对正确的架构进行了编译:

$ lipo -info /opt/local/lib/libboost_locale-mt.a
input file /opt/local/lib/libboost_locale-mt.a is not a fat file
Non-fat file: /opt/local/lib/libboost_locale-mt.a is architecture: x86_64

另外,我在文件中搜索了 from_utf 并且出现了以下符号:

$ otool -tv /opt/local/lib/libboost_locale-mt.a | grep from_utf
__ZN5boost6locale4conv8from_utfIcEENSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEPKT_SC_RKS9_NS1_11method_typeE:

使用c++filt 解开它后,我可以验证它与链接器未找到的符号相同(除非 __1 意味着什么?):

$ c++filt __ZN5boost6locale4conv8from_utfIcEENSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEPKT_SC_RKS9_NS1_11method_typeE
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > boost::locale::conv::from_utf<char>(char const*, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, boost::locale::conv::method_type)

所以,我现在几乎一无所知。

你们能帮我找出问题所在,或者至少我还能尝试什么吗?

提前致谢。

【问题讨论】:

  • __1 确实意味着什么。这意味着它来自 clang 的 libc++,而不是 g++ 使用的 libstdc++。

标签: c++ macos boost linker g++


【解决方案1】:

看起来您的boost::locale 是用clang 和libc++(Mac OS X 上的默认编译器和标准库)编译的,而您的程序是用g++ 和libstdc++(g++ 的标准库)编译的。这两个标准库不是二进制兼容的。

您需要使用 clang 和 libc++ 编译所有内容,或者获取使用 g++ 和 libstdc++ 编译的 boost 版本。

【讨论】:

  • 很好,非常感谢!当引发提升异常时,我也得到了 EXC_BAD_ACCESS。现在我明白为什么了。除了中间那个晦涩的“__1”命名空间之外,还有什么方法可以在未来检查这一点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 2013-11-18
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多