2

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?

3
  • The error is unrelated to GCC. The Cython processing step has failed leaving a file that is designed to cause a compile error. Commented Dec 22, 2016 at 22:49
  • Can you elaborate @DavidW? Commented Dec 22, 2016 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 .pyxit 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. Commented Dec 22, 2016 at 23:50

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.