6

I'm working on a project to call C++ from Python. We use Cython for this purpose. When compile by using the command "python3.6 setup.py build_ext --inplace", the compiler "x86_64-linux-gnu-gcc" is used. Is there a way to use a different compiler like "arm-linux-gnueabihf-g++"?

Also, is there a way to add a compilation option such as "-DPLATFORM=linux"?

Here's the setup.py:

from distutils.core import setup, Extension
from Cython.Build import cythonize

setup(ext_modules = cythonize(Extension(
    "app",
    sources=["app.pyx", "myapp.cpp"],
    language="c++",
    include_dirs=["../base"]
)))
Akshay Prabhakar
  • 430
  • 4
  • 13
user1556331
  • 345
  • 2
  • 12
  • Have a look at this https://stackoverflow.com/questions/38388812/using-cython-to-cross-compile-project-from-intel-ubuntu-to-arm – altunyurt Jul 30 '19 at 14:07
  • 1
    Does this answer your question? [How to tell distutils to use gcc?](https://stackoverflow.com/questions/16737260/how-to-tell-distutils-to-use-gcc) – ead Dec 15 '19 at 10:17

3 Answers3

2

Distutils by default uses the CC system environment variable to decide what compiler to use. You can run the python script such that the CC variable is set to whatever you want at the beginning of the script before setup() is called.

As for passing flags to the compiler, add a extra_compile_args named argument to your Extension() module. It may look like this for example:

from distutils.core import setup, Extension
from Cython.Build import cythonize

setup(ext_modules = cythonize(Extension(
    "app",
    sources=["app.pyx", "myapp.cpp"],
    language="c++",
    include_dirs=["../base"],
    extra_compile_args=["-DPLATFORM=linux"]
)))
JSON Brody
  • 736
  • 1
  • 4
  • 23
2

You can fix the value of the CC environment variable in setup.py. For instance:

os.environ["CC"] = "g++"

or

os.environ["CC"] = "clang++"
Rexcirus
  • 2,459
  • 3
  • 22
  • 42
1

In order to specify a C++ compiler for Cython, you need to set a proper CXX environment variable prior calling setup.py.

This could be done:

  • Using a command-line option:
export CXX=clang++-10
  • In the setup.py:
os.environ["CXX"] = "clang++-10"

Note: clang++-10 is used as an example of the alternative C++ compiler.

Note: CC environment variable specifies C compiler, not C++. You may consider specifying also e.g. export CC=clang-10.

Ales Teska
  • 1,198
  • 1
  • 17
  • 38