Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify C++11 with distutils?

Tags:

I have a module that needs to be compiled with C++11. On GCC and Clang, that means a std=c++11 switch, or std=c++0x on older compilers.

Python is not compiled with this switch so Distutils doesn't include it when compiling modules.

What is the preferred way to compile C++11 code with distutils?

like image 429
Adam Avatar asked Apr 24 '14 06:04

Adam


People also ask

What does Distutils do in Python?

The distutils package provides support for building and installing additional modules into a Python installation. The new modules may be either 100%-pure Python, or may be extension modules written in C, or may be collections of Python packages which include modules coded in both Python and C.

Where do I put setup py?

To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.

What is Python Setuptools used for?

Setuptools is a package development process library designed to facilitate packaging Python projects by enhancing the Python standard library distutils (distribution utilities). Essentially, if you are working with creating and distributing Python packages, it is very helpful.


2 Answers

You can use the extra_compile_args parameter of distutils.core.Extension:

ext = Extension('foo', sources=[....],
                libraries=[....], 
                extra_compile_args=['-std=c++11'],
                ....)

Note that this is completely platform dependent. It won't even work on some older versions of gcc and clang.

like image 104
juanchopanza Avatar answered Oct 17 '22 10:10

juanchopanza


You can override the default values for various Distutils compilation and link flags using environment variables. This may require some experimentation depending on which platform you are on and how the Python you was using was built. But generally overriding CFLAGS will affect the compilation phase and either one of LDSHARED or LDFLAGS will affect the link phase.

export CFLAGS='-std=c++11'
pip install blah

or

export CFLAGS='-std=c++11'
python setup.py install

On OS X, another option is to use the ARCHFLAGS environment variable which has the advantage of not wiping out the original CFLAGS or LDSHARED values.

like image 40
Ned Deily Avatar answered Oct 17 '22 09:10

Ned Deily