Home

CMAKECXXFLAGS

CMAKE_CXX_FLAGS is a CMake cache variable that stores extra command-line options to pass to the C++ compiler for all targets built by a project. It serves as the CMake analogue to CMAKE_C_FLAGS for C, and is commonly used to apply uniform warnings, standards, or other global compiler options across targets. In modern CMake, it is considered a legacy mechanism, and per-target options are preferred, but CMAKE_CXX_FLAGS remains widely seen in older projects.

Usage and behavior: CMAKE_CXX_FLAGS is a single string. You can append flags by modifying the variable, for

Per-configuration and generator considerations: CMake also defines per-configuration variants such as CMAKE_CXX_FLAGS_DEBUG, CMAKE_CXX_FLAGS_RELEASE, CMAKE_CXX_FLAGS_RELWITHDEBINFO, and CMAKE_CXX_FLAGS_MINSIZEREL.

Examples and best practices: A typical practice in older projects is to append common flags via CMAKE_CXX_FLAGS,

See also: CMAKE_C_FLAGS, CMAKE_CXX_STANDARD, target_compile_options, add_compile_options.

example
using
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS}
-Wall
-Wextra").
The
flags
are
applied
to
all
C++
targets
unless
overridden
by
other
means.
For
more
explicit
control,
modern
projects
use
target_compile_options
or
add_compile_options
to
attach
flags
to
specific
targets.
These
are
applied
according
to
the
build
configuration
on
single-configuration
generators
(like
Makefile
or
Ninja).
On
multi-configuration
generators
(such
as
Visual
Studio
or
Xcode),
these
per-configuration
variables
map
to
the
corresponding
configurations,
but
the
recommended
approach
is
to
use
generator-feature
aware
methods
like
target_compile_options
with
configurable
expressions
where
possible.
e.g.,
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS}
-std=c++17
-Wall").
However,
contemporary
CMake
guidelines
favor
setting
the
language
standard
with
CMAKE_CXX_STANDARD
and
using
target_compile_options
or
target_compile_features
to
apply
flags
on
a
per-target
basis,
which
improves
portability
and
maintainability.