EagleDad
(Eagle Dad)
October 9, 2022, 4:01pm
1
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.
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
ben.boeckel
(Ben Boeckel (Kitware))
October 9, 2022, 9:47pm
2
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?
EagleDad
(Eagle Dad)
October 10, 2022, 6:51am
3
Hi Ben,
thanks for the feedback.
Here is a minimum example that shows the behavior.
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
ben.boeckel
(Ben Boeckel (Kitware))
October 10, 2022, 1:23pm
4
It looks like the -O2
flag comes from CMAKE_CXX_FLAGS_RELEASE
, so that variable would need to be edited:
string(REPLACE "/O2" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
EagleDad
(Eagle Dad)
October 10, 2022, 1:53pm
5
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
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
EagleDad
(Eagle Dad)
October 10, 2022, 2:51pm
6
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.
ben.boeckel
(Ben Boeckel (Kitware))
October 10, 2022, 6:00pm
7
Hmm. I suspect this is just CMake not knowing what fields correlate with Intel compiler flags that well. Please file an issue about this.
EagleDad
(Eagle Dad)
October 11, 2022, 5:55am
8
Thanks for your hints right now.
The issue is created and can be found here:
https://gitlab.kitware.com/cmake/cmake/-/issues/24045