# Proper way to use OpenMP: target\_link\_libraries() enough?

**URL:** https://discourse.cmake.org/t/proper-way-to-use-openmp-target-link-libraries-enough/9158
**Category:** Code
**Created:** [October 9, 2023, 4:24pm UTC](https://discourse.cmake.org/t/proper-way-to-use-openmp-target-link-libraries-enough/9158 "2023-10-09T16:24:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![szhorvat](https://discourse.cmake.org/user_avatar/discourse.cmake.org/szhorvat/32/1906_2.png) [@szhorvat](https://discourse.cmake.org/u/szhorvat)
#### Post date: [October 9, 2023, 4:24pm UTC](https://discourse.cmake.org/t/proper-way-to-use-openmp-target-link-libraries-enough/9158/1 "2023-10-09T16:24:20Z")

</div>

With a project that uses OpenMP, is it sufficient to set `target_link_libraries(... OpenMP::OpenMP_C)` or do we need to set compiler options (in addition to linker options) separately?

The following test project works fine:

```auto
cmake_minimum_required(VERSION 3.27)

project(foo)

find_package(OpenMP)

add_executable(foo main.c)
target_link_libraries(foo PRIVATE OpenMP::OpenMP_C)

```

`-fopenmp` is added both when compiling the sources and when linking the object files. I expected `target_link_libraries` to only affect linker flags, not compiler flags. How do the compiler flags get added, and how could I figure out from the CMake docs that `target_link_libraries` is sufficient?

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [October 9, 2023, 7:21pm UTC](https://discourse.cmake.org/t/proper-way-to-use-openmp-target-link-libraries-enough/9158/2 "2023-10-09T19:21:51Z")

</div>

> How do the compiler flags get added

That happens in [FindOpenMP](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/FindOpenMP.cmake#L104) module, which is called when you execute `find_package(OpenMP)`.

> how could I figure out from the CMake docs that `target_link_libraries` is sufficient

Well, that’s a fair amount of docs that you would need to read to get to that understanding. In short, I’d say that this part of the documentation wouldn’t be specific to OpenMP, as this topic is rather common and applies to any library/package in general.

The `target_link_libraries()` is sufficient when the library files are already discovered, so you can provide them to this function as arguments, but obviously you first need to discover those files, and that is what `find_package()` does (_among other things, as you can see in that `FindOpenMP` module_). You can write your own discovery procedure which wouldn’t set `-fopenmp` flags, if that is what you are after. Alternatively, perhaps you could also overwrite those flags right after `find_package(OpenMP)` line.

---

<div class="post-metadata">

### Author: ![szhorvat](https://discourse.cmake.org/user_avatar/discourse.cmake.org/szhorvat/32/1906_2.png) [@szhorvat](https://discourse.cmake.org/u/szhorvat)
#### Post date: [October 9, 2023, 7:43pm UTC](https://discourse.cmake.org/t/proper-way-to-use-openmp-target-link-libraries-enough/9158/3 "2023-10-09T19:43:01Z")

</div>

Looking more carefully, it is this bolded part of the [`target_link_libraries()` documentation](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) that I should have paid more attention to:

> Specify libraries or flags to use when linking a given target and/or its dependents. **[Usage requirements](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#target-usage-requirements) from linked library targets will be propagated. Usage requirements of a target’s dependencies affect compilation of its own sources.**

Correct?

---

<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: [October 9, 2023, 11:38pm UTC](https://discourse.cmake.org/t/proper-way-to-use-openmp-target-link-libraries-enough/9158/4 "2023-10-09T23:38:57Z")

</div>

Yes, that is correct. Usage requirements include “what is needed to use this target” and can indeed involve compilation flags.
