How to find OpenMP with clang on macOS

All,

I’m feeling really stupid right now. Not sure what I’m doing wrong. First, I’m a Fortran programmer, so I am not good at C. But, when I try and use OpenMP and Clang, I can’t seem to. Many things on line say I need to install libomp from brew but:

❯ brew install libomp
Warning: libomp 16.0.6 is already installed and up-to-date.
To reinstall 16.0.6, run:
  brew reinstall libomp

So I have it install. But CMake never seems to see OpenMP. I kept whittling down my CMakeLists.txt until I got to:

cmake_minimum_required(VERSION 3.24)
project(test LANGUAGES C)
find_package(OpenMP)

with clang I get:

❯ cmake -B build . -DCMAKE_C_COMPILER=clang
-- The C compiler identification is AppleClang 14.0.3.14030022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES)
-- Could NOT find OpenMP (missing: OpenMP_C_FOUND)
-- Configuring done (2.0s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/mathomp4/OpenMPTester/build

Is there something special I need to set in CMake to get this to work? Or is this just “Apple Clang be broken, sorry”?

If it matters, I’m on a M2 MacBook. Maybe this is an Arm thing?

I think it’s an Xcode thing. See this page.

@ben.boeckel Thanks for the pointer. For so long on my machines I’ve built (by hand) gcc. But in trying to make it easier for others I thought “Let’s just use clang for C”.

I’ll work with my admins and see if they can install that for testing. If we install it, does CMake have the magic inside to find and use it?

I have no idea. I don’t remember why that is in my browser history, but I don’t remember the results (if any) of testing its results.

Need to set OpenMP_ROOT on macOS to point to libomp. Details: OpenMP with CMake · GitHub

1 Like

Ahh nice! Now the next fun I’ll be having is: why do I need to do this only on my M2 laptop but not my M1 Studio. They each have the same OS version, compilers, Xcode, CMake version…all identical. But on one, it’s needed, the other it’s not. So weird.

Maybe one has an environment variable like CMAKE_PREFIX_PATH or OpenMP_ROOT set already?

Or Homebrew is loaded by default on the M1 machine? I’d diff env output on each just to make sure.

My dotfiles setup is identical all around as are the modulefiles (modulo compiler licenses). But, in the end, the OpenMP_ROOT fix might as well be applied to both for consistency!

Now I’m having fun with trying Intel on my M1/M2 under Rosetta2. It all works pretty well save…OpenMP! :smile: My guess is the libomp from brew is not for x86 so an ifort/clang/clang++ setup under Rosetta2 can’t handle that. And my tests of trying to build things with ifort/icc/icpc some libraries got VERY confused:

ld: warning: ignoring file ./.libs/libnco.a, building for macOS-x86_64 but attempting to link with file built for unknown-x86_64

But since Intel on Arm Mac is more of an experiment to “just try to get things good enough” so I can locally build/debug a library with Intel Fortran, and I don’t need C/C++ OpenMP…I’m okay with not caring too much. MPI works, OpenMP Fortran works…so I’m good.

Yeah, I’ve tried Intel oneAPI on Apple Silicon via Rosetta, and there are a lot of problems beyond just the one you are mentioning (not related to CMake).

For me the environment variable OpenMP_ROOT does not work. I need
cmake -DOpenMP_CXX_FLAG="-Xclang -fopenmp" -DOpenMP_CXX_INCLUDE_DIR=/opt/homebrew/opt/libomp/include -DOpenMP_CXX_LIB_NAMES=libomp -DOpenMP_C_FLAG="-Xclang -fopenmp" -DOpenMP_C_INCLUDE_DIR=/opt/homebrew/opt/libomp/include -DOpenMP_C_LIB_NAMES=libomp -DOpenMP_libomp_LIBRARY=/opt/homebrew/opt/libomp/lib/libomp.dylib

This is for a freshly installed mac, where brew instals since Oct 2022 to /opt/homebrew/opt/libomp/. On my other (older system, also arm64), it is /opt/homebrew and I have no issues.

Is it the same CMake version on both? Please file an issue if it is either:

  • new OpenMP in Homebrew isn’t being found; or
  • new CMake doesn’t detect the OpenMP.

If it is an older CMake that doesn’t detect it and a newer CMake works…upgrade? :slight_smile:

cmake 3.28.1 with /opt/homebrew/opt/libomp does not work,
cmake 3.26.4 with /opt/homebrew does work

I found the existing issue and added some information
https://gitlab.kitware.com/cmake/cmake/-/issues/24097