Search code examples
macoscmakeclangopenmp

OMP library not found in /usr/local/lib via cmake


MacOS Sonoma 14.2.1, Apple M2

I'm trying to compile GMSHFEM via CMake. The program uses OpemMP, which caused some problems (since CMake can't find HomeBrew's OpenMP by default), but eventually, I solved these by copying the appropriate header files "omp.h" to /usr/local/include and library "libomp.dylib" to /usr/local/lib.

-- Found OpenMP_CXX: -Xclang -fopenmp (found version "5.0") 
-- Found OpenMP: TRUE (found version "5.0") 

Now, CMake can find OpenMP, but on running the 'make' command, I get the error:

[100%] Linking CXX shared library libgmshfem.dylib
ld: library 'omp' not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I don't understand this. How can the omp library not be found if it's sitting in /usr/local/lib?

Opening CMakeCache.txt, I see:

//CXX compiler flags for OpenMP parallelization
OpenMP_CXX_FLAGS:STRING=-Xclang -fopenmp

//Path to a file.
OpenMP_CXX_INCLUDE_DIR:PATH=/usr/local/include

//CXX compiler libraries for OpenMP parallelization
OpenMP_CXX_LIB_NAMES:STRING=libomp

//Path to a library.
OpenMP_libomp_LIBRARY:FILEPATH=/usr/local/lib/libomp.dylib

So, it appears that everything is in order. What am I doing wrong that clang can't link with the omp library properly?


Solution

  • The issue was resolved by pointing GMSHFEM's CMake instructions to the correct homebrew directories, which for me were:

      elseif(APPLE AND EXISTS "/opt/homebrew/lib")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include")
        list(APPEND EXTRA_LIBS "-L/opt/homebrew/opt/libomp/lib -lomp")
        set_config_option("OpenMP[Homebrew,M1]" HAVE_OPENMP)
      else()
    

    For some reason, the pre-compiled OpenMP libraries I downloaded and placed in /usr/local/lib may have been the issue, although my knowledge of OpenMP and CMake is extremely limited.

    @Alex Reinking appears to be correct, but it looks like GMSHFEM was indeed already linking to OpenMP_CXX.