【问题标题】:Linker error for functions expecting const std::string &期望 const std::string & 的函数的链接器错误
【发布时间】:2016-04-18 07:20:26
【问题描述】:

在尝试链接到共享库 (gtkmm) 时,我收到签名包含 std::string const& 的函数的链接器错误。

例如,如果函数声明为

void set_icon_from_file(const std::string&);

g++ 报告

undefined reference to `Gtk::Window::set_icon_from_file(std::string const&)'

我正在使用来自pkg-config 的链接器标志,而这不会发生在其他函数(例如将原始类型作为参数的函数)中,所以我怀疑链接器配置正确。

我的目标文件上的readelf 给了我

45: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND _ZN3Gtk6Window18set_icon_from_fileERKSs

在共享库上

10991: 00000000003c0950    96 FUNC    GLOBAL DEFAULT   11 _ZN3Gtk6Window18set_icon_from_fileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

我不知道该怎么做。我检查了函数在库头文件和源代码中是否使用相同的签名声明。由于名称中的__cxx11 部分,我首先怀疑C++ 版本之间存在兼容性问题,但是,我也在使用C++11 构建。

【问题讨论】:

  • 在 C++11 最终确定时,GNU libstdc++ 发生了一些变化。问题可能是你的 gtkmm 是用旧的编译器编译的。

标签: c++ c++11 gcc linker


【解决方案1】:

看看我的 c++-filt 输出:

_ZN3Gtk6Window18set_icon_from_fileERKSs
Gtk::Window::set_icon_from_file(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
_ZN3Gtk6Window18set_icon_from_fileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Gtk::Window::set_icon_from_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)

表示第二个版本编译时启用了glibcxx11 abi。 Here 更多相关信息。

所以,基本上你需要将你的 gcc 更新到 5.1 版本或降级库。

【讨论】:

  • 太好了,一个简单的编译器更新实际上解决了这个问题。我也不知道c++filt,好用的小工具!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多