# How to find OpenMP with clang on macOS

**URL:** https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860
**Category:** Usage
**Tags:** os:macos, comp:clang
**Created:** [September 1, 2023, 12:29pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860 "2023-09-01T12:29:21Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [September 1, 2023, 12:29pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/1 "2023-09-01T12:29:21Z")

</div>

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:

```auto
❯ 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
cmake_minimum_required(VERSION 3.24)
project(test LANGUAGES C)
find_package(OpenMP)

```

with clang I get:

```auto
❯ 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?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 1, 2023, 1:31pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/2 "2023-09-01T13:31:01Z")

</div>

I think it’s an Xcode thing. See [this page](https://mac.r-project.org/openmp/).

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [September 1, 2023, 1:46pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/3 "2023-09-01T13:46:29Z")

</div>

@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?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 1, 2023, 4:26pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/4 "2023-09-01T16:26:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [September 1, 2023, 8:41pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/5 "2023-09-01T20:41:57Z")

</div>

Need to set OpenMP\_ROOT on macOS to point to libomp. Details: [OpenMP with CMake · GitHub](https://gist.github.com/scivision/16c2ca1dc250f54d34f1a1a35596f4a0)

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [September 5, 2023, 11:54am UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/6 "2023-09-05T11:54:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [September 5, 2023, 10:58pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/7 "2023-09-05T22:58:57Z")

</div>

Maybe one has an environment variable like CMAKE\_PREFIX\_PATH or OpenMP\_ROOT set already?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 6, 2023, 12:17pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/8 "2023-09-06T12:17:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [September 6, 2023, 3:30pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/9 "2023-09-06T15:30:38Z")

</div>

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! 😄 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:

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

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [September 6, 2023, 7:43pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/10 "2023-09-06T19:43:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Fabian188](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fabian188/32/2934_2.png) [@Fabian188](https://discourse.cmake.org/u/Fabian188)
#### Post date: [December 20, 2023, 9:58pm UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/11 "2023-12-20T21:58:32Z")

</div>

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.

> <https://github.com/Homebrew/homebrew-core/issues/112107>
>
> \### \`brew gist-logs \<formula\>\` link OR \`brew config\` AND \`brew doctor\` output
> 
> \`…\`\`shell
> HOMEBREW\_VERSION: 3.6.3
> ORIGIN: https://github.com/Homebrew/brew
> HEAD: d5b6b068c15a8df60f0f5e34a4ca80dcf82c48e3
> Last commit: 5 days ago
> Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
> Core tap HEAD: 3d00b39d454e973773f178f5541b1f651912fa65
> Core tap last commit: 30 hours ago
> Core tap branch: master
> HOMEBREW\_PREFIX: /opt/homebrew
> HOMEBREW\_CASK\_OPTS: \[\]
> HOMEBREW\_DISPLAY: /private/tmp/com.apple.launchd.IKLaHGlLyB/org.xquartz:0
> HOMEBREW\_MAKE\_JOBS: 10
> Homebrew Ruby: 2.6.8 =\> /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
> CPU: 10-core 64-bit arm\_firestorm\_icestorm
> Clang: 13.0.0 build 1300
> Git: 2.32.0 =\> /Library/Developer/CommandLineTools/usr/bin/git
> Curl: 7.77.0 =\> /usr/bin/curl
> macOS: 12.0.1-arm64
> CLT: 13.2.0.0.1.1638488800
> Xcode: N/A
> Rosetta 2: false
> 
> $ brew doctor
> Please note that these warnings are just used to help the Homebrew maintainers
> with debugging if you file an issue. If everything you use Homebrew for is
> working fine: please don't worry or file an issue; just ignore this. Thanks!
> 
> Warning: A newer Command Line Tools release is available.
> Update them from Software Update in System Preferences or run:
> softwareupdate --all --install --force
> 
> If that doesn't show you any updates, run:
> sudo rm -rf /Library/Developer/CommandLineTools
> sudo xcode-select --install
> 
> Alternatively, manually download them from:
> https://developer.apple.com/download/all/.
> You should download the Command Line Tools for Xcode 13.4.
> 
> 
> Warning: Some installed kegs have no formulae!
> This means they were either deleted or installed manually.
> You should find replacements for the following formulae:
> szip
> 
> Warning: Unbrewed dylibs were found in /usr/local/lib.
> If you didn't put them there on purpose they could cause problems when
> building Homebrew formulae, and may need to be deleted.
> 
> Unexpected dylibs:
> /usr/local/lib/libspimage.1.dylib
> 
> Warning: Unbrewed header files were found in /usr/local/include.
> If you didn't put them there on purpose they could cause problems when
> building Homebrew formulae, and may need to be deleted.
> 
> Unexpected header files:
> /usr/local/include/spimage.h
> /usr/local/include/spimage/colormap.h
> /usr/local/include/spimage/cuda\_util.h
> /usr/local/include/spimage/fft.h
> /usr/local/include/spimage/find\_center.h
> /usr/local/include/spimage/hashtable.h
> /usr/local/include/spimage/image.h
> /usr/local/include/spimage/image\_filter.h
> /usr/local/include/spimage/image\_filter\_cuda.h
> /usr/local/include/spimage/image\_io.h
> /usr/local/include/spimage/image\_noise.h
> /usr/local/include/spimage/image\_sphere.h
> /usr/local/include/spimage/image\_util.h
> /usr/local/include/spimage/interpolation\_kernels.h
> /usr/local/include/spimage/linear\_alg.h
> /usr/local/include/spimage/list.h
> /usr/local/include/spimage/map.h
> /usr/local/include/spimage/mem\_util.h
> /usr/local/include/spimage/phasing.h
> /usr/local/include/spimage/prtf.h
> /usr/local/include/spimage/sperror.h
> /usr/local/include/spimage/statistics.h
> /usr/local/include/spimage/support\_update.h
> /usr/local/include/spimage/time\_util.h
> \`\`\`
> 
> 
> \### Verification
> 
> \- \[X\] I ran \`brew update\` and am still able to reproduce my issue.
> \- \[X\] I have resolved all warnings from \`brew doctor\` and that did not fix my problem.
> 
> \### What were you trying to do (and why)?
> 
> To be able to compile OpenMP programs with both clang and gcc on a system with \`libomp\` installed.
> 
> \### What happened (include all command output)?
> 
> \`libomp\` installs \`omp.h\` in \`/opt/homebrew/include/omp.h\` 
> When compiling an OpenMP program which includes \`omp.h\` with \`gcc\` and using other header files in \`/opt/homebrew/include/\` \`gcc\` will use libomp's header file as it takes priority over gcc's built-in omp.h file.
> But that header file is incompatible with gcc's \`libgomp\` resulting in a broken program.
> 
> You the same issue in this \[bug report\](https://bugzilla.redhat.com/show\_bug.cgi?id=1451138) for the \`libomp\` RPM package for Fedora.
> 
> I also came across this issue \[here\](https://github.com/3dem/relion/issues/913). 
> 
> \### What did you expect to happen?
> 
> The omp.h from libomp should not be used when compiling OpenMP code with gcc.
> 
> \### Step-by-step reproduction instructions (by running \`brew\` commands)
> 
> \`\`\`shell
> Here's a small example of the problem: https://gist.github.com/biochem-fan/31864239460769d2a4a3585e4959d298
> \`\`\`

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 21, 2023, 1:50am UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/12 "2023-12-21T01:50:38Z")

</div>

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? 🙂

---

<div class="post-metadata">

### Author: ![Fabian188](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fabian188/32/2934_2.png) [@Fabian188](https://discourse.cmake.org/u/Fabian188)
#### Post date: [December 21, 2023, 7:26am UTC](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860/13 "2023-12-21T07:26:16Z")

</div>

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](https://gitlab.kitware.com/cmake/cmake/-/issues/24097)
