# Intel oneApi C++ Compiler with Visual Studio 2022

**URL:** https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634
**Category:** Usage
**Tags:** os:windows, comp:intel
**Created:** [October 9, 2022, 4:01pm UTC](https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634 "2022-10-09T16:01:40Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![EagleDad](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eagledad/32/2818_2.png) [@EagleDad](https://discourse.cmake.org/u/EagleDad)
#### Post date: [October 9, 2022, 4:01pm UTC](https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634/1 "2022-10-09T16:01:40Z")

</div>

Hello,

I’m using CMake 3.23.1 with Visual Studio 2022 and the Intel oneApi C++ Compiler 2022. Everything works fine, but I have some trouble with changing some compiler flags for individual projects.

I want to change the optimization from O2 to O3 but I’m not able to get it work. And I also want do enable the optimization output from the Intel compiler.

Overwriting the `CMAKE_CXX_FLAGS` does not work.

```auto
set(CMAKE_CXX_FLAGS_RELEASE "/MD /O3 /DNDEBUG /O3 /Qopt-report:3 " PARENT_SCOPE)

```

Adding compile option by `add_compile_options("/Qopt-report:3")` also does not work.

I also tried to set options specific by target, but it also does not work `target_compile_options( ${LIBRARY_NAME_RAW} PUBLIC /O3)`.

What am I doing wrong?

Thanks and best regards,  
Dirk

---

<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, 2022, 9:47pm UTC](https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634/2 "2022-10-09T21:47:16Z")

</div>

The `PARENT_SCOPE` seems odd to me. Can you reproduce this in a simple example just to make sure nothing else in the project is interfering?

---

<div class="post-metadata">

### Author: ![EagleDad](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eagledad/32/2818_2.png) [@EagleDad](https://discourse.cmake.org/u/EagleDad)
#### Post date: [October 10, 2022, 6:51am UTC](https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634/3 "2022-10-10T06:51:52Z")

</div>

Hi Ben,

thanks for the feedback.

Here is a minimum example that shows the behavior.

```auto
project(IntelTest)

set(EXECUTABLE_NAME intelTest)

cmake_minimum_required(VERSION 3.22)

set(CMAKE_CONFIGURATION_TYPES "Debug;Release")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std:c++17 -Qopt-report:3 -O3")

add_executable(${EXECUTABLE_NAME}
    main.cpp
)

```

The C++ standard is set, but the optimization flag is still /O2

---

<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 10, 2022, 1:23pm UTC](https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634/4 "2022-10-10T13:23:18Z")

</div>

It looks like the `-O2` flag comes from `CMAKE_CXX_FLAGS_RELEASE`, so that variable would need to be edited:

```cmake
string(REPLACE "/O2" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")

```

---

<div class="post-metadata">

### Author: ![EagleDad](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eagledad/32/2818_2.png) [@EagleDad](https://discourse.cmake.org/u/EagleDad)
#### Post date: [October 10, 2022, 1:53pm UTC](https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634/5 "2022-10-10T13:53:02Z")

</div>

I found something similar and now I think it is an issue with the Intel C++ compiler and Visual Studio.

Visual Studio support Od, O1, O2 and Ox.  
The Intel compiler supports Od, O1, O2, Ox and O3[Intel C++].

With

```auto
MESSAGE(STATUS "CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")
string(REGEX REPLACE "/O[0-4]" "/O3" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
MESSAGE(STATUS "CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")

```

I can change to all possible values for the optimization that are supported by Visual Studio, but I cannot set the value to O3, also the CXX\_FLAGS in the CMake window state that O3 is set. In Visual Studio it is still O2

---

<div class="post-metadata">

### Author: ![EagleDad](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eagledad/32/2818_2.png) [@EagleDad](https://discourse.cmake.org/u/EagleDad)
#### Post date: [October 10, 2022, 2:51pm UTC](https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634/6 "2022-10-10T14:51:08Z")

</div>

If I use Ninja to build the project, the flags for optimization are accepted. So I think there must be a connection to Visual Studio. Changing the values in Visual Studio directly work, but not with CMake.

---

<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 10, 2022, 6:00pm UTC](https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634/7 "2022-10-10T18:00:22Z")

</div>

Hmm. I suspect this is just CMake not knowing what fields correlate with Intel compiler flags that well. Please file [an issue](https://gitlab.kitware.com/cmake/cmake/-/issues) about this.

---

<div class="post-metadata">

### Author: ![EagleDad](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eagledad/32/2818_2.png) [@EagleDad](https://discourse.cmake.org/u/EagleDad)
#### Post date: [October 11, 2022, 5:55am UTC](https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634/8 "2022-10-11T05:55:43Z")

</div>

Thanks for your hints right now.

The issue is created and can be found here:  
[https://gitlab.kitware.com/cmake/cmake/-/issues/24045](https://gitlab.kitware.com/cmake/cmake/-/issues/24045)
