Skip to content

Instantly share code, notes, and snippets.

@jharmer95
Last active June 10, 2021 18:05
Show Gist options
  • Save jharmer95/97528c7034a449af257bbfe05426a351 to your computer and use it in GitHub Desktop.
Save jharmer95/97528c7034a449af257bbfe05426a351 to your computer and use it in GitHub Desktop.
Flags for C++ compilers
if(WIN32)
set(TARGET_WINDOWS TRUE)
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(CXX_MSVC TRUE)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CXX_MINGW TRUE)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CXX_CLANG TRUE)
endif()
elseif(UNIX AND NOT APPLE)
set(TARGET_LINUX TRUE)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CXX_GCC TRUE)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CXX_CLANG TRUE)
endif()
elseif(APPLE)
set(TARGET_APPLE TRUE)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CXX_GCC TRUE)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CXX_CLANG TRUE)
endif()
endif()
if(CXX_MSVC)
set(PROJECT_CXX_FLAGS
/W4
/external
/W0
/external:anglebrackets
/permissive-
/wd4619
/w14242
/w14254
/w14263
/w14265
/w14287
/we4289
/w14296
/w14311
/w14545
/w14546
/w14547
/w14549
/w14555
/w14640
/w14826
/w14905
/w14906
/w14928
/we4834)
elseif(CXX_GCC OR CXX_MINGW)
set(PROJECT_CXX_FLAGS
-fuse-ld=lld
-pipe
-Wall
-Wextra
-Wpedantic
-Wshadow
-Wnon-virtual-dtor
-Wold-style-cast
-Wcast-align
-Wunused
-Woverloaded-virtual
-Wconversion
-Wsign-conversion
-Wnull-dereference
-Wdouble-promotion
-Wformat=2
-Weffc++
-Wuseless-cast
-Wlogical-op
-Wduplicated-branches
-Wduplicated-cond
-Wmisleading-indentation
-Wswitch-enum
-Werror=format-security
-Wl,-z,defs)
elseif(CXX_CLANG)
set(PROJECT_CXX_FLAGS
-fuse-ld=lld
-pipe
-Weverything
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-c++98-c++11-compat
-Wno-c++98-c++11-compat-binary-literal
-Wno-c++98-c++11-c++14-compat
-Wno-exit-time-destructors
-Wno-missing-prototypes
-Wno-documentation
-Wno-documentation-unknown-command
-Werror=format-security
-Wl,-z,defs)
endif()
if(CXX_GCC OR CXX_MINGW OR CXX_CLANG)
set(PROJECT_DEBUG_FLAGS
-g3
-gsplit-dwarf)
set(PROJECT_RELEASE_FLAGS
-flto)
set(PROJECT_EXE_FLAGS
-fno-semantic-interposition
-Wl,--as-needed
-Wl,-Bsymbolic-functions)
set(PROJECT_SO_FLAGS
-fvisibility=hidden
-fno-semantic-interposition
-Wl,--as-needed
-Wl,-Bsymbolic-functions
-Wl,-z,defs)
set(PROJECT_HARDEN_FLAGS
-O2
-D_FORTIFY_SOURCE=2
-D_GLIBCXX_ASSERTIONS
-fasynchronous-unwind-tables
-fno-plt
-fstack-clash-protection
-fstack-protector-strong
-Wl,-z,relro
-fcf-protection
-Wl,--no-copy-dt-needed-entries)
endif()
# For every project
## Clang
-fuse-ld=lld
-pipe
-Weverything
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-c++98-c++11-compat
-Wno-c++98-c++11-compat-binary-literal
-Wno-c++98-c++11-c++14-compat
-Wno-exit-time-destructors
-Wno-missing-prototypes
-Wno-documentation
-Wno-documentation-unknown-command
-Werror=format-security
-Wl,-z,defs
## GCC
-fuse-ld=lld
-pipe
-Wall
-Wextra
-Wpedantic
-Wshadow
-Wnon-virtual-dtor
-Wold-style-cast
-Wcast-align
-Wunused
-Woverloaded-virtual
-Wconversion
-Wsign-conversion
-Wnull-dereference
-Wdouble-promotion
-Wformat=2
-Weffc++
-Wuseless-cast
-Wlogical-op
-Wduplicated-branches
-Wduplicated-cond
-Wmisleading-indentation
-Wswitch-enum
-Werror=format-security
-Wl,-z,defs
## MSVC
/W4
/external
/W0
/external:anglebrackets
/permissive-
/wd4619
/w14242
/w14254
/w14263
/w14265
/w14287
/we4289
/w14296
/w14311
/w14545
/w14546
/w14547
/w14549
/w14555
/w14640
/w14826
/w14905
/w14906
/w14928
/we4834
# For debug builds
## GCC/Clang
-g3
-gsplit-dwarf
# For release builds
## GCC/Clang
-flto
# For executables
## GCC/Clang
-fno-semantic-interposition
-Wl,--as-needed
-Wl,-Bsymbolic-functions
# For shared libraries
## GCC/Clang
-fvisibility=hidden
-fno-semantic-interposition
-Wl,--as-needed
-Wl,-Bsymbolic-functions
-Wl,-z,defs
# For hardened builds
## GCC/Clang
-D_FORTIFY_SOURCE=2
-D_GLIBCXX_ASSERTIONS
-fasynchronous-unwind-tables
-fpie -Wl,-pie (for executables)
-fpic -shared (for shared libraries)
-fno-plt
-fplugin=annobin (optional, requires annobin)
-fstack-clash-protection
-fstack-protector-strong
-Wl,-z,relro
-fcf-protection
-Wl,--no-copy-dt-needed-entries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment