Fabian Knorr
Fabian Knorr

Reputation: 3194

Linker error for functions expecting const std::string &

When trying to link against a shared library (gtkmm), I get linker errors for functions whose signature contains a std::string const&.

For example, if the function is declared as

void set_icon_from_file(const std::string&);

g++ reports

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

I am using the linker flags from pkg-config, and this does not happen for other functions (functions taking primitive types as arguments, for example), so I suspect the linker is configured properly.

readelf on my object file gives me

45: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND _ZN3Gtk6Window18set_icon_from_fileERKSs

and on the shared library

10991: 00000000003c0950    96 FUNC    GLOBAL DEFAULT   11 _ZN3Gtk6Window18set_icon_from_fileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

I'm not sure what to make of that. I checked that the functions are declared with the same signature in both the library headers and source. Because of the __cxx11 part in the mangled name, I first suspected a compatibility issue between C++ versions, however, I'm building with C++11 as well.

Upvotes: 4

Views: 1569

Answers (1)

ivaigult
ivaigult

Reputation: 6667

Look at my c++-filt output:

_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&)

It means that the second version is compiled with glibcxx11 abi enabled. Here more information about it.

So, basically you need to update your gcc to 5.1 version or downgrade the library.

Upvotes: 3

Related Questions