1

I'm using OSX v10.11.6 with a recent version of xcode installed. So my default compiler is gcc, which is really clang. I have used homebrew to install gcc5 so that I can use openMP, and by setting CC := g++-5 in my Makefiles for my source code in C, I can successfully compile C source code with non-trivial usage of -fopenmp.

What I want to do is get Cython to compile with gcc5 so that I can use Cython's native prange feature, as demonstrated in a minimal example here. I have written a minimal example in this gist, borrowed from the Neal Hughes page. When I attempt to compile omp_testing.pyx using setup.py, I get a (possibly unrelated) warning, and fatal error:

cc1plus: warning: command line option '-Wstrict-prototypes' is valid for C/ObjC but not for C++
omp_testing.cpp:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
 #error Do not use this file, it is the result of a failed Cython compilation.
  ^
error: command 'g++-5' failed with exit status 1

After reading How to tell distutils to use gcc?, what I attempted was setting the CC environment variable inside setup.py, but this did not work. How should I modify my Cython setup.py file to compile using g++-5?

Community
  • 1
  • 1
aph
  • 1,765
  • 2
  • 19
  • 34
  • The error is unrelated to GCC. The Cython processing step has failed leaving a file that is designed to cause a compile error. – DavidW Dec 22 '16 at 22:49
  • Can you elaborate @DavidW? – aph Dec 22 '16 at 23:00
  • `setup.py` does (at least) two things. It uses Cython to process your `.pyx` file to a `.c` file and it then it uses your C compiler to compile the C file. If Cython fails to process the `.pyx`it will produce some helpful error output telling you why it's unhappy and it will produce a `omp_testing.c` file that contains an `#error` line which tells any C compiler to stop. You should see some additional error messages when you run `setup.py` (the first few are usually most helpful). Failing that you can run `cython omp_testing.pyx` from the command line yourself to see what's wrong. – DavidW Dec 22 '16 at 23:50

1 Answers1

1

Apparently, Apple dropped the support of OpenMP sometime ago, therefore, you cannot compile the code that includes this dependency with a standard gcc. A good way to get around that is to install LLVM and compile with it. Here is the sequence that worked for me:

Install LLVM:

brew install llvm

Include OpenMP flags(-fopenmp -lomp) to setup.py:

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


exts = [Extension(name='name_of_your_module',
                  sources=['your_module.pyx'],
                  extra_compile_args=['-fopenmp'],
                  extra_link_args=['-lomp']
                  )]

import numpy as np

setup(name = 'name_of_your_module',
      ext_modules=cythonize(exts,
      include_dirs=[np.get_include()],
      cmdclass={'build_ext': build_ext})

And then compile the code with LLVM:

CC=/usr/local/opt/llvm/bin/clang++ python setup.py build_ext --inplace

This should result in a parallelized .so

  • How can you explain this when Apple doesn't provide the OpenMP developer libs? Have to build them first or install via Brew, or do they get pulled in together with llvm?I know OpenMP is a subproject of LLVM, but I thought you had to compile the dev files from source and include manually... – C. Sederqvist Aug 31 '20 at 23:46