2

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! Initially I tried python3-config --includes, but the computer couldn't find such a file in the usr/bin directory (but there was a python2-config and a python-config,so I assumed the latter meant python 3 - will try to find out why this is the case). I'll try to implement your suggestions - Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.