Okay, let's break this down because CMake's flag handling has a few layers.
Short Answer:
No, cmake -DCMAKE_CXX_FLAGS="-Ofast" does not directly remove flags that CMake might add from other sources (like CMAKE_CXX_FLAGS_RELEASE for a Release build). It replaces the content of the CMAKE_CXX_FLAGS variable itself. The final set of flags passed to the compiler is a combination.
Detailed Explanation:
CMake uses several variables to construct the final compiler flags:
-
CMAKE_CXX_FLAGS: This is a general-purpose variable for C++ flags that apply to all build types (Debug, Release, etc.) by default. When you use cmake -DCMAKE_CXX_FLAGS="-Ofast", you are setting this specific variable to "-Ofast". If CMAKE_CXX_FLAGS had any previous value (e.g., set in CMakeLists.txt or from a previous configure), your command line definition will override it for this configuration run.
-
CMAKE_CXX_FLAGS_<BUILD_TYPE>: These are flags specific to a particular build type. For example:
CMAKE_CXX_FLAGS_DEBUG (e.g., -g)
CMAKE_CXX_FLAGS_RELEASE (e.g., -O3 -DNDEBUG)
CMAKE_CXX_FLAGS_RELWITHDEBINFO (e.g., -O2 -g -DNDEBUG)
CMAKE_CXX_FLAGS_MINSIZEREL (e.g., -Os -DNDEBUG)
How They Combine:
Typically, CMake concatenates these flags. For a given target, the flags passed to the C++ compiler will roughly be:
flags_from_target_properties (like target_compile_options) + CMAKE_CXX_FLAGS_<BUILD_TYPE> + CMAKE_CXX_FLAGS
(The exact order and mechanism can sometimes be influenced by project structure and CMake version, but this is the general idea).
What Happens with Your Command:
If you run:
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-Ofast" ..
- CMake will look at
CMAKE_CXX_FLAGS_RELEASE. Let's say by default it's "-O3 -DNDEBUG".
- CMake will see you've set
CMAKE_CXX_FLAGS to "-Ofast".
- The compiler will likely receive flags similar to:
... -O3 -DNDEBUG -Ofast ...
Consequences and Potential Issues:
- Overriding Optimization Levels: In the example above, you have both
-O3 (from CMAKE_CXX_FLAGS_RELEASE) and -Ofast (from your CMAKE_CXX_FLAGS). Compilers usually take the "strongest" or last specified optimization flag that covers others. Since -Ofast implies -O3 plus other, potentially unsafe, optimizations, the compiler will effectively use -Ofast. It might even issue a warning about conflicting optimization flags.
- Losing Other General Flags: If
CMAKE_CXX_FLAGS was previously set in your CMakeLists.txt (e.g., set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")) to add warning flags, your command line -DCMAKE_CXX_FLAGS="-Ofast" would overwrite that, and you'd lose -Wall from CMAKE_CXX_FLAGS. The -Wall would only be present if it was also part of CMAKE_CXX_FLAGS_RELEASE or added via target_compile_options.
Better Approach (Usually):
If you want to use -Ofast specifically for your Release builds, it's generally better to modify the build-type specific flags:
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS_RELEASE="-Ofast -DNDEBUG" ..
This way:
- Your Debug builds will still use their default debug flags.
CMAKE_CXX_FLAGS can still be used for flags common to all build types (like -Wall, -Wextra).
- You are more explicit about which build type gets
-Ofast.
How to Check:
To see the exact commands CMake generates, you can run make (or ninja) with verbosity:
make VERBOSE=1
or
ninja -v
This will show you the full compiler invocation, including all flags.
In Summary:
cmake -DCMAKE_CXX_FLAGS="-Ofast" sets the CMAKE_CXX_FLAGS variable. It doesn't remove flags from CMAKE_CXX_FLAGS_<BUILD_TYPE>, but the final combined flags might lead to -Ofast overriding other optimization levels due to compiler behavior. For build-type-specific optimizations, modifying CMAKE_CXX_FLAGS_<BUILD_TYPE> is often cleaner.