HLL
HLL

Reputation: 415

Pybind11 error undefined reference to `Py_GetVersion'

I'm trying to get Pybind 11 to work and am struggling. I tried to compile my .cpp file using this:

c++  -Wall -std=c++11 -fPIC `python3 -m --includes` -I ~/Documents/Single_electron/pybind11/include single_e.cpp -o single_e `python-config --includes`

But get a whole host of errors, starting with this list:

 single_e.cpp :(.text+0xaa): undefined reference to `Py_GetVersion'
 single_e.cpp :(.text+0x104): undefined reference to `PyExc_ImportError'
 single_e.cpp :(.text+0x123): undefined reference to `PyErr_Format'
 single_e.cpp :(.text+0x1b2): undefined reference to `PyExc_ImportError'
 single_e.cpp :(.text+0x1c0): undefined reference to `PyErr_SetString'
 single_e.cpp :(.text+0x1f9): undefined reference to `PyExc_ImportError'
 single_e.cpp :(.text+0x207): undefined reference to `PyErr_SetString'

Has anyone got any suggestions about how I should fix this?

Upvotes: 2

Views: 3644

Answers (1)

Peter
Peter

Reputation: 14927

You're pulling python includes more than one way

python3 -m --includes

and

python-config --includes

The first shouldn't even run (do you mean python3-config --includes?), and in any case, you need to decide whether you want python2 or python3, and only use one.

Second, you've only handled the include directories for python. You still need to tell the compiler how to link the libraries, which is the core of your problem. That's another call to python-config: python-config --ldflags

Finally, you're better off with --cflags to grab the include directories, as that will also set a bunch of other flags so that your compiled code matches python's conventions. This includes -fPIC for example.

So, to wrap it up:

c++ -std=c++11 `python-config --cflags` -I ~/Documents/Single_electron/pybind11/include single_e.cpp -o single_e `python-config --ldflags`

of course substituting the right name for your python-config, such as python3-config if that is fact your target.

Upvotes: 2

Related Questions