materials
materials copied to clipboard
Specifying python.h and pybind11 path in windows
Hello everyone, and thanks for this great material.
I am trying to use cython to import a C++ library in Python.
I followed the instructions in Python Bindings: Calling C or C++ From Python, but I have a problem.
Everything is fine till I run "invoke build-cython"
The g++ cannot find the path for python and pybind11 directory. The error is:
I guess the line ""-I /usr/include/python3.7 -I . "" in tasks.py file (line 132) does not work for me. Am I right? (is it because I'm using Windows?) Can anyone please help me to solve this issue?
Thank you in advance. Vahid.
is it because I'm using Windows?
I think so. It was written for Linux and hasn't been tested on windows. Which is why I'm surprised if you got the PyBind11 stuff to work. I guess you could maybe try something like mingw. Haven't tried it myself though.
Thank you so much @Znunu , Is it possible to create the .py file using Linux, and then call it from a Python environment in Windows?
Binaries must be built for either win or linux. Running the tasks from the tutorial on linux and then just copying them over to win, won't work.
Frankly I'm not very knowledgeable on the topic of compilers and linkers. It might be worth asking someone else. You can also look for dedicated pybind11 tutorials.
Hi, @Vahid44
Znunu is correct that building on Linux and copying to windows will be unsucessful. There are a few options for you. One is to use LSW (Linux Subsystem For Windows) or Minix (which runs under windows). The other involves getting the code to compile using the MS compiler.
There are two places to look to get this working. First, look at the work that @Znunu added to tasks.py from line 47-57. This is what sets up and runs the compiler for the cmult example on Windows. You'll need to add something similar to the compile_python_module()
function lower in that file.
The code that is there currently uses some fancy tricks to run python to generate the command line:
invoke.run(
"g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
"`python3 -m pybind11 --includes` "
"-I /usr/include/python3.7 -I . "
"{0} "
"-o {1}`python3.7-config --extension-suffix` "
"-L. -lcppmult -Wl,-rpath,.".format(cpp_name, extension_name)
)
The two interesting sections are python3 -m pybind11 --includes
(which is in backticks), and python3.7-config --extension-suffix
(also in backticks). You might need to run these as separate calls and gather the output to format the full compiler call.
I'm sorry, but I don't have a windows machine on which to experiment for this. I hope this helps!