Rob Rob - 1 month ago 10
Python Question

Python Extension with openmp in OS X

I have created a python extension that works correctly in Linux but doesn't work in os x.

The C code makes use of the openmp library.

The C compiler that is preinstalled in os x is clang and the preinstalled python has been built with clang. That clang doesn't support openmp.

I can compile and run my code by installing gcc-6 by:

brew install gcc


I can build my libary by including in the file setup.py the environment variable CC:

os.environ["CC"]="/usr/local/Cellar/gcc/6.2.0/bin/gcc-6"


And I include the gcc library path in the setup configuration:

library_dirs = ["/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/","../build/"],


But then I obtain errors when I load my library from python:

import myLibraryName


ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/LIBIRWLS.so, 2): Symbol not found: ___emutls_get_address
Referenced from: /usr/local/opt/gcc/lib/gcc/6/libgomp.1.dylib
Expected in: /usr/lib/libSystem.B.dylib
in /usr/local/opt/gcc/lib/gcc/6/libgomp.1.dylib

What should I do? What is the correct procedure create a python extension in OS X that makes use of the openmp library?

Rob Rob
Answer

I thik I have solved this issue. This is the project I am working on:

https://github.com/RobeDM/LIBIRWLS

In resume:

1 - I installed gcc6 by

brew install gcc

2 - Then I declared the path to gcc in a environment variables in the file setup.py:

os.environ["CC"]=/usr/local/Cellar/gcc/6.2.0/bin/gcc-6

3 - I left the CFLAGS environment variable empty:

os.environ["CFLGAS"]=""

4 - In the extension module I included the file libgomp.a associated to gcc6 and I used the extra ling arg "static":

ext_modules = [ 
    Extension('LIBIRWLS',
        include_dirs=['.','../include/',np.get_include()],
        sources = ['myexample.c'],
        extra_objects = ["/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/libgomp.a",
            ....Object files of my library
        ],
        library_dirs = ["../build/"],
        extra_compile_args=['-Wno-cpp','-static','-lgomp','-lblas','-llapack'],
        extra_link_args=['-static']
    )
    ]

setup( name = 'LIBNAME', version = '1.0', description="My extension description", install_requires=[ 'numpy', 'scipy', ], url='https://myurl.com', ext_modules = ext_modules )