'OpenMP linker -lgomp library not found on M1 mac

My issue is with the openmp not working with a C Extension in a python setup.py file.

I am running this code and I have a setup.py file as below:

from setuptools import *

libTM = Extension('libTM',
                  sources = ['pyTsetlinMachineParallel/ConvolutionalTsetlinMachine.c', 'pyTsetlinMachineParallel/MultiClassConvolutionalTsetlinMachine.c', 'pyTsetlinMachineParallel/Tools.c'],
                  include_dirs=['pyTsetlinMachineParallel'],
                  extra_compile_args=['-fopenmp'],
                  extra_link_args=['-lgomp'])

setup(
   name='pyTsetlinMachineParallel',
   version='0.2.1',
   ]
)

Now when I compile this using python3 setup.py build , I get a clang-13 error:

ld: library not found for -lgomp
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)

I know openmp is installed on my mac (installed with Homebrew) because I can run the OpenMP test code with the following Makefile:

CPP = /usr/local/opt/llvm/bin/clang
CPPFLAGS = -I/usr/local/opt/llvm/include -fopenmp
LDFLAGS = -L/usr/local/opt/llvm/lib

omp_hello: omp_hello.c
    $(CPP) $(CPPFLAGS) $^ -o $@ $(LDFLAGS)

This is the result if I do echo $PATH:

/usr/local/opt/llvm/lib:/usr/local/opt/llvm/include:/usr/local/opt/llvm/bin:/usr/local/opt/[email protected]/bin:/Users/Saram/bin:/usr/local/bin:/usr/local/opt/llvm/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands

ANY help would be appreciated, I have tried nearly everything I can find on StackOverflow and I am really struggling. Thank you.



Solution 1:[1]

If you are linking with -lgomp, the linker will look for a libgomp.* file.

First, try to locate the file with the find command. For my centos machine, it is in /usr/lib64, which I found with the following command: find /usr -name libgomp*.

Then, you have to indicate the location of that directory with either -L compiler option, or setting the LD_LIBRARY_PATH environment variable (Note that PATH is not checked for libs, as others pointed out).

Also, just check if you have libomp.* instead, and try to link with it using -lomp to see if it works.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nimish Shah