-1

I have a C++ project which uses a couple of c++14 features including std::make_unique.

The project compiles and runs fine and has done for a while however, now I am trying to add a python interface and I'm having some troubles.

In my python extension I try to declare my C++ class as unique using:

#include <memory>
...
typedef struct {
    PyObject_HEAD
    std::unique_ptr<MyClass> my_instance;
} PyMyClass;
...
self->my_instance = std::make_unique<MyClass>();

And in my setup.py file I have included -std=c++14 as a compiler option.

It builds fine with python setup.py develop/install but when I import the module into python I get the following error:

my_module.so: undefined symbol: _Z15build_331792650RSt10unique_ptrIN6grelka5SlaveESt14default_deleteIS1_EE

Do I need to do an extra linking step somewhere?

JMzance
  • 1,704
  • 4
  • 30
  • 49
  • 1
    Can you please give the error message associated with your simplified example, instead of the one from your actual code? In this case I think you might have simplified it to the point the problem doesn’t show up; that’s why people often ask for [examples that are both complete and verifiable](https://stackoverflow.com/help/mcve), so they can reproduce the error and investigate. In any case, the symbol it’s complaining about is `build_331792650(std::unique_ptr >&)` (obtained from the `c++filt` program); does that mean anything to you? – Daniel H Aug 17 '17 at 16:51
  • 2
    Here's a useful [tool](https://demangler.com/) to demangle that symbol online. And yes as @Daniel mentioned provide a [MCVE] that reproduces that error please. – user0042 Aug 17 '17 at 16:56
  • Since it compiles and links fine, it is most likely to do with how python searches for dynamic libraries (.so) at runtime. Did you update the environment variable so it can find your libraries? – Anon Mail Aug 17 '17 at 17:03
  • 1
    As noted, the undefined symbol is a *function* (which name implies it's kind of automatically generated) and not anything to do with `std::unique_ptr` or `std::make_unique` itself. How do you interface with Python? What do you know about `std::unique_ptr`? – Some programmer dude Aug 17 '17 at 17:03
  • Thanks @DanielH you pointed me in the right direction (I was missing a file which contained the definition of that function) and thanks user0042 for that tool - which will help find these in future! – JMzance Aug 17 '17 at 17:03

1 Answers1

1

To summarize the discussion in the comments:

This is a linker error, meaning that there’s some function or object declared in your code, but you aren’t linking to any object file which defines it. If you use a name demangling tool like c++filt or demangler.com, you see that the missing symbol isn’t anything that was part of unique_ptr itself, but the function build_331792650.

This looks like it might be auto-generated (at least I hope so, because if you’re writing functions with this sort of name you should probably rethink your naming scheme), but either way, you need to link in the file which contains the definition of this function.

Daniel H
  • 7,223
  • 2
  • 26
  • 41