# Unique way to enable OpenMP with CMake?

**URL:** https://discourse.cmake.org/t/unique-way-to-enable-openmp-with-cmake/10350
**Category:** Code
**Created:** [March 12, 2024, 7:19pm UTC](https://discourse.cmake.org/t/unique-way-to-enable-openmp-with-cmake/10350 "2024-03-12T19:19:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![asimplecoder](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/f17d59/32.png) [@asimplecoder](https://discourse.cmake.org/u/asimplecoder)
#### Post date: [March 12, 2024, 7:19pm UTC](https://discourse.cmake.org/t/unique-way-to-enable-openmp-with-cmake/10350/1 "2024-03-12T19:19:33Z")

</div>

Hi everyone,

I am wondering which is the recommended way to enable `OpenMP` support, regardless of the OS.

I mostly work on Windows OS, and there it was only “sufficient” to set the target compile options to include the required flag.  
I have very recently started to move to UNIX OSes, and there I realised that was not working anymore (failing at link time). So, I went for the `find_package` route. Solved. But then, this is apparently not working on Windows again, in which CMake is unable to find OpenMP.

This approach is also mentioned [here](https://discourse.cmake.org/t/proper-way-to-use-openmp-target-link-libraries-enough/9158/2). [Here](https://discourse.cmake.org/t/how-to-find-openmp-with-clang-on-macos/8860) is a similar question, but for MacOS.

So. What is the best (and unique?) way to enable OpenMP support? I would say using the intrinsic module provided `FindOpenMP`, but I don’t understand why it is not working in this case.

The code:

```auto
      find_package( OpenMP )
      if ( NOT OpenMP_Fortran_FOUND )
         message( FATAL_ERROR "OpenMP not found" )
      endif()
      target_link_libraries( ${project} PUBLIC OpenMP::OpenMP_Fortran )

```

Thanks.

---

<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: [March 12, 2024, 7:35pm UTC](https://discourse.cmake.org/t/unique-way-to-enable-openmp-with-cmake/10350/2 "2024-03-12T19:35:41Z")

</div>

Yes, doing something like:

```cmake
find_package(OpenMP REQUIRED)

```

is the canonical way. Indeed, if you add `REQUIRED` then there is no need to test if found, CMake will stop at that point if it can’t find OpenMP support.

You then do:

```cmake
target_link_libraries( target <PUBLIC|PRIVATE> OpenMP::OpenMP_Fortran )

```

to link to it (CMake will also “pass down” any includes as well this way so you don’t need any `target_include_directories`).

If you know you only need Fortran you can also do:

```auto
find_package(OpenMP REQUIRED COMPONENTS Fortran)

```

As far as I know, this should be OS agnostic.

---

<div class="post-metadata">

### Author: ![asimplecoder](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/f17d59/32.png) [@asimplecoder](https://discourse.cmake.org/u/asimplecoder)
#### Post date: [March 13, 2024, 6:20am UTC](https://discourse.cmake.org/t/unique-way-to-enable-openmp-with-cmake/10350/3 "2024-03-13T06:20:39Z")

</div>

Thanks @mathomp4 .

Yet, it does not work with

```cmake
      find_package( OpenMP REQUIRED COMPONENTS Fortran )
      target_link_libraries( ${project} PUBLIC OpenMP::OpenMP_Fortran )

```

It still fails:

```plaintext
CMake Error at C:/Program Files/CMake/share/cmake-3.29/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find OpenMP_Fortran (missing: OpenMP_Fortran_FLAGS
  OpenMP_Fortran_LIB_NAMES)
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.29/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
  C:/Program Files/CMake/share/cmake-3.29/Modules/FindOpenMP.cmake:581 (find_package_handle_standard_args)
  src/CMakeLists.txt:123 (find_package)

```

---

<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: [March 14, 2024, 11:00pm UTC](https://discourse.cmake.org/t/unique-way-to-enable-openmp-with-cmake/10350/4 "2024-03-14T23:00:07Z")

</div>

Hmm. I guess next question: what is your Fortran compiler? Maybe it’s a compiler CMake does not know about?

I think if you know the exact flag used by the Fortran compiler, you can specify it with:

```auto
cmake -DOpenMP_Fortran_FLAG="-flag" ...

```

---

<div class="post-metadata">

### Author: ![asimplecoder](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/f17d59/32.png) [@asimplecoder](https://discourse.cmake.org/u/asimplecoder)
#### Post date: [March 15, 2024, 7:03am UTC](https://discourse.cmake.org/t/unique-way-to-enable-openmp-with-cmake/10350/5 "2024-03-15T07:03:27Z")

</div>

> [@mathomp4](#):
>
> I guess next question: what is your Fortran compiler? Maybe it’s a compiler CMake does not know about?

Mh, I doubt, since it is the `ifort` Intel compiler. On UNIX it works, while it crashes on Windows.  
The same using the new `ifx` compiler.

---

<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: [March 15, 2024, 2:47pm UTC](https://discourse.cmake.org/t/unique-way-to-enable-openmp-with-cmake/10350/6 "2024-03-15T14:47:34Z")

</div>

Well then, looking at Gitlab and seeing this:

[https://gitlab.kitware.com/cmake/cmake/-/commit/d427bfae61bfba8dca41c4d3c11f0f67c2583f96](https://gitlab.kitware.com/cmake/cmake/-/commit/d427bfae61bfba8dca41c4d3c11f0f67c2583f96)

I think it’s time to mention @brad.king

Maybe it’s possible you have just a perfectly wrong version of CMake 3.29?
