Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying python.h and pybind11 path in windows #169

Open
Vahid44 opened this issue Nov 15, 2020 · 4 comments
Open

Specifying python.h and pybind11 path in windows #169

Vahid44 opened this issue Nov 15, 2020 · 4 comments

Comments

@Vahid44
Copy link

Vahid44 commented Nov 15, 2020

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:
Issue1

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.

@Vahid44 Vahid44 changed the title Specifing python.h and pybind11 path in windows Specifying python.h and pybind11 path in windows Nov 15, 2020
@Znunu
Copy link
Contributor

Znunu commented Nov 15, 2020

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.

@Vahid44
Copy link
Author

Vahid44 commented Nov 15, 2020

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?

@Znunu
Copy link
Contributor

Znunu commented Nov 15, 2020

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.

@jima80525
Copy link
Contributor

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants