# Disable OpenMP

**URL:** https://discourse.cmake.org/t/disable-openmp/2700
**Category:** Usage
**Tags:** os:linux
**Created:** [February 5, 2021, 6:58am UTC](https://discourse.cmake.org/t/disable-openmp/2700 "2021-02-05T06:58:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![pauljurczak](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pauljurczak/32/1418_2.png) [@pauljurczak](https://discourse.cmake.org/u/pauljurczak)
#### Post date: [February 5, 2021, 6:58am UTC](https://discourse.cmake.org/t/disable-openmp/2700/1 "2021-02-05T06:58:14Z")

</div>

I’m using CMake v3.19.3 on Ubuntu 18.04.5 with clang 11.0.1 and VS Code. Here is my CMakeLists.txt file:

```auto
cmake_minimum_required(VERSION 3.18)
project(ocv VERSION 0.1.0)

find_package(fmt REQUIRED)
find_package(OpenCV 4.5 REQUIRED)
find_package(OpenMP REQUIRED)

add_executable(ocv main.cpp grid.cpp)

include_directories(
  /home/paul/include
  /home/paul/eigen-3.3.9
  /home/paul/eigen-3.3.9/unsupported
  /home/paul/Open3D/3rdparty/tinyfiledialogs/include
  /home/paul/boost_1_75_0
)

target_precompile_headers(ocv PRIVATE ocv.hpp)

target_compile_options(ocv PRIVATE -O3 -fopenmp -Wno-narrowing)
# target_compile_options(ocv PRIVATE -O3 -fno-openmp -Wno-narrowing)

target_link_libraries(ocv 
  fmt::fmt
  ${OpenCV_LIBS}
  OpenMP::OpenMP_CXX
)

set(CMAKE_VERBOSE_MAKEFILE TRUE)
set_property(TARGET ocv PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_COMMAND} -E time")
set_property(TARGET ocv PROPERTY CXX_STANDARD 20)

```

Regardless which of the two `target_compile_options` lines above I choose, the target is compiled with OpenMP code generated. What is the correct way to turn OpenMP on and off.

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [February 5, 2021, 7:50am UTC](https://discourse.cmake.org/t/disable-openmp/2700/2 "2021-02-05T07:50:11Z")

</div>

Have you tried removing `OpenMP::OpenMP_CXX` from the `target_link_libraries` call?

---

<div class="post-metadata">

### Author: ![pauljurczak](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pauljurczak/32/1418_2.png) [@pauljurczak](https://discourse.cmake.org/u/pauljurczak)
#### Post date: [February 5, 2021, 8:04am UTC](https://discourse.cmake.org/t/disable-openmp/2700/3 "2021-02-05T08:04:45Z")

</div>

Yes, I tried. It produces linker error:

```auto
[build] /usr/bin/ld: CMakeFiles/ocv.dir/grid.cpp.o: undefined reference to symbol 'omp_get_thread_num@@OMP_1.0'
[build] //usr/lib/x86_64-linux-gnu/libgomp.so.1: error adding symbols: DSO missing from command line

```

when used with `-fopenmp`. Works fine with `-fno-openmp`, but I would rather have a less drastic solution for switching between serial and parallel versions of the project. Is there a single compiler switch, or CMake variable, which can do the job?

---

<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: [February 5, 2021, 2:59pm UTC](https://discourse.cmake.org/t/disable-openmp/2700/4 "2021-02-05T14:59:50Z")

</div>

I think using/not using `OpenMP::OpenMP_CXX` should be enough. It comes with the `-fopenmp` flag attached to its use already, so you shouldn’t need to do anything with the flag (additionally, its spelling can change based on the compiler in use; the target already accounts for that).

---

<div class="post-metadata">

### Author: ![pauljurczak](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pauljurczak/32/1418_2.png) [@pauljurczak](https://discourse.cmake.org/u/pauljurczak)
#### Post date: [February 5, 2021, 7:40pm UTC](https://discourse.cmake.org/t/disable-openmp/2700/5 "2021-02-05T19:40:07Z")

</div>

Thank you. It does work. I wonder though, if this will hold for more complicated OpenMP flags like device offload, e.g. `–fopenmp-targets=nvptx64-nvidia-cuda`. I will test it when I get to that stage.

---

<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: [February 5, 2021, 7:45pm UTC](https://discourse.cmake.org/t/disable-openmp/2700/6 "2021-02-05T19:45:36Z")

</div>

Hmm. I don’t think so. It’s something that could be added to `FindOpenMP` though (either as a target or a function to make the flags based on other information). Please open [an issue](https://gitlab.kitware.com/cmake/cmake/issues) to track such an enhancement as `openmp-targets` currently doesn’t appear anywhere in CMake’s codebase.

Cc: @robert.maynard

---

<div class="post-metadata">

### Author: ![pauljurczak](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pauljurczak/32/1418_2.png) [@pauljurczak](https://discourse.cmake.org/u/pauljurczak)
#### Post date: [February 5, 2021, 9:23pm UTC](https://discourse.cmake.org/t/disable-openmp/2700/7 "2021-02-05T21:23:09Z")

</div>

Done.

---

<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: [February 5, 2021, 9:27pm UTC](https://discourse.cmake.org/t/disable-openmp/2700/8 "2021-02-05T21:27:52Z")

</div>

Thanks. For reference: [https://gitlab.kitware.com/cmake/cmake/-/issues/21784](https://gitlab.kitware.com/cmake/cmake/-/issues/21784)
