Return include and runtime library directories from Python

Suppose I want to use gcc from the command line to compile a Python C extension. I would structure the call like this:

 gcc -o applesauce.pyd -IC:/Python35/include -LC:/Python35/libs -l python35 applesauce.c 

I noticed that the -I , -L and -L options are absolutely necessary, otherwise you will get an error message. These commands tell gcc where to look for headers ( -I ), where to look for static libraries ( -L ) and which static library to use on actually ( python35 , which actually translates to libpython35.a ).

Now, it is really very easy to get the libs and include directories if this is your machine, as they never change unless you want to. However, I wrote a program that calls gcc from the command line that other people will use. The line in which this call occurs looks something like this:

 from subprocess import call import sys filename = applesauce.c include_directory = os.path.join(sys.exec_prefix, 'include') libs_directory = os.path.join(sys.exec_prefix, 'libs') call(['gcc', ..., '-I', include_direcory, '-L', libs_directory, ...]) 

However, others will have different platforms and different Python installation structures, so just connecting to them will not always work.

Instead, I need a solution from Python that will reliably return include and libs directories.

Edit:

I looked at the distutils.ccompiler module and found many useful functions that distutils would use in part, but make it customizable for me so that my compiler completely steps over the platform. The only thing I need is to pass the include and runtime libraries to it ...

Edit 2:

I looked at distutils.sysconfig and I can reliably return the include directory, including all header files. I still don't know how to get the runtime library.

The distutils.ccompiler docs distutils.ccompiler here

A program that needs this feature is called Cyther.

+5
source share
2 answers

If you look at the source of build_ext.py from distutils in the finalize_options method, you will find the code for the different platforms used to search for libs.

+1
source

The easiest way to compile extensions is to use distutils like this ;

 from distutils.core import setup from distutils.extension import Extension setup(name='foobar', version='1.0', ext_modules=[Extension('foo', ['foo.c'])], ) 

Keep in mind that, compared to unix / linux, compiling extensions in ms-windows is not direct , since different versions of the ms compiler are closely related to the corresponding C library. So, on ms-windows you need to compile extensions for Python xy with the same compiler that Python xy was compiled with. This means using old and unsupported tools, for example. Python 2.7, 3.3, and 3.4. The situation is changing ( part 1 , part 2 ) with Python 3.5.

Edit: Having examined the problem more, I doubt that what you want is 100% achievable. Given that the tools required for compilation and linking are, by definition, outside of Python, and that they are not even necessarily the tools that Python is compiled with, the information in sys and sysconfig not guaranteed by the accurate representation of the system that Python is actually installed. For instance. most ms-windows machines will not have developer tools installed. And even on POSIX platforms, there may be a difference between the installed C compiler and the compiler that Python built, especially when it is installed as a binary package.

+5
source

Source: https://habr.com/ru/post/1246183/


All Articles