8

I'm trying to create pybind11 bindings for an existing cmake project. The CMakeLists.txt file looks like the one in the tutorial. The project builds without errors, however, when trying to import the module in ipython, the following error comes up:

~/workspace/a/build/pya.cpython-35m-x86_64-linux-gnu.so: undefined symbol: _ZN3a13FooC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

Trying to solve it: It seems related to the toolchain (this issue looks similar). I've gcc 6.5.0 and cmake 3.12.0 installed.

Armin Meisterhirn
  • 801
  • 1
  • 13
  • 26
  • It seems that you didn't properly link something. Unfortunately the names in C++ are sometimes nog very descriptive, but sometimes you recognise part of the name to give you a hint what you did not link. To be able to help you we need a minimal reproducible example here. – Tom de Geus Dec 05 '18 at 07:21
  • It seems you did not put the cpp file of the `_ZN3a13FooC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE` function to the compile command. – GoingMyWay May 16 '22 at 09:07
  • Additionally, check closely that the function signature in your .h file matches the one in your .cpp file. – thayne Aug 21 '23 at 20:00

1 Answers1

14

This is harder to answer than necessary, the linker error message is obfuscated. Use an online demangler to see the plaintext symbol name that the linker cannot find. Be sure to copy/paste the real mangled symbol.

A valid mangled name that somewhat resembles the error message would be _ZN1a3FooC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE. Which demangles to a::Foo::Foo(const std::string&).

In other words, you declared a constructor for the Foo class but forgot to write it. Pretty standard mistake. More about these linker errors in this Q+A.

jpihl
  • 7,941
  • 3
  • 37
  • 50
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536