'Clion compile with -O3

I'm writing a c++ program using CLion and I need to specify -O3 flag on the compiler, using set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" -O3) on the CMakeList file does not work. Is there a way to do it?



Solution 1:[1]

Use either add_compile_options(-O3) to add it globally or target_compile_options(YourTarget -O3) to add it locally to a specific target.


You could also do it by using CMAKE_CXX_FLAGS but that is a pretty old way of doing things in CMakeLists files, that's how it would look like: set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") or set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -O3).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1