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

Build binary distribution with c/c++ extensions #4848

Closed
reox opened this issue Nov 7, 2018 · 6 comments
Closed

Build binary distribution with c/c++ extensions #4848

reox opened this issue Nov 7, 2018 · 6 comments

Comments

@reox
Copy link

reox commented Nov 7, 2018

I read #3599 on using an AfterBuild target to invoke setup.py but this does not solve my question about building binary distributions including some c/c++ extension.
As I read in the documentation, you should setup a new project containing the C++ code.
Usually, I would write in the setup.py something like

from setuptools import setup, Extension

setup(
    name="exttest",
    version="0.0.1",
    packages=["PythonTest", ],
    scripts=["script.py", ],
    ext_modules = [Extension("PythonTest.PythonExtensionTest", sources=["PythonExtensionTest.c"])],
    )

but as PythonExtensionTest is now another project, this does not work.
I tried to add PythonExtensionTest into packages but then python tells me, that this package does not exsist.
script.py btw contains a simple test:

print("Hello World")

import PythonTest
print(PythonTest.__version__)

import PythonExtensionTest
print(PythonExtensionTest.__version__)

and I can run this program directly from VS2017, as PythonExtensionTest.pyd is build correctly, but is in the wrong path, as I would like to have it as a module under PythonTest.

Is there any way to build binary distributions which include c/c++ extensions directly from visual studio?
Or is the preferred way to use VS just for development and have setup.py along with the package? But how can I then use setup.py correctly, If I have the modules in separate folders?

For example, specifiying the extensions like:

    ext_modules = [Extension("PythonExtensionTest", sources=["../PythonExtensionTest/PythonExtensionTest.c"])],

works, but I loose all the control over the build process now, which I gained using a VS Project.

thanks in advance!

@huguesv
Copy link
Contributor

huguesv commented Nov 10, 2018

I don't really have much real world experience developing native modules, so I might be giving you bad advice here, or just be misunderstanding the way you have things organized, but I'll give you some ideas...

You could have PythonExtensionTest.c in the location that you want if you were not using VS ie your "usually" case.

Either put the .vcxproj in the same directory of the .c file, or somewhere else that includes the .c file.

Meaning, you don't have to stick to what VS gives you by default as far as solution/project folder organization.

It is fine to have your .pyproj, .vcxproj both in the same folder, or to have projects include source files from a separate folder from where the project files are located.

For .pyproj, there's a HomeFolder property that can be used to specify the root of all sources. It defaults to '.' but the "From Existing Python" new project wizard sets it to something else if you decide to create the .pyproj in a location different from the sources.

C++ projects can also easily include sources from alternate folder. That, with changes to the C++ linker settings to control the output location would hopefully let you keep the .c file where you want it and have the binary created in the right place.

As far as "build binary distribution" that is something that we always do from command line / build server, not the IDE.

@reox
Copy link
Author

reox commented Nov 10, 2018

Thanks for the answer!

As far as "build binary distribution" that is something that we always do from command line / build server, not the IDE.

Yes, this is actually what we want to do. We have a build server environment, where we specify the project and target. Then msbuild is called and the resulting files are collected.
We can also call python setup.py bdist_whatever, but then the MSVC toolset is not set correctly, thus the building of the extensions fails.

Putting both the pyproj and vcxproj into the same folder might help, I need to check!

@reox
Copy link
Author

reox commented Nov 20, 2018

I played around with this for a while now. I'm now able to build the projects by using multipe vcxproj files and some pre and postbuild rules to invoke python for packaging.

Inside the python documentation, I also found the hint to look at the VS project for cpython: https://github.com/python/cpython/tree/3.7/PCbuild

@reox reox closed this as completed Nov 20, 2018
@Smascer
Copy link

Smascer commented Sep 20, 2023

as in example from Microsoft Im trying to build c++ extension for python as python module
my problem is that Im using CLR support for C++. my c++ Project can be build with Visual studio. and also is usable in python without problems.
If I try to build it with python setup.py build I see that msvc config is wrong.
I tried to use extra args but it does not overwrite the compiler call.
It is possible to use .vcxproj somehow while setup.py? best way could be to call msbuild.
@reox it looks like you was able to build vcxproj, can you maybe share your solution?

@reox
Copy link
Author

reox commented Sep 21, 2023

@Smascer unfortunately, I do not have access to these files anymore and quite some time has passed since I played around with that... afair, I simply used the vxproj files from cpython as a reference.

@Smascer
Copy link

Smascer commented Sep 21, 2023

@reox thank you for a quick answer. I will take a look on cpython.

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