Template cmake
cmake_minimum_required (VERSION 3.23)
#--- v1.0.0 ---
#------------------------------------------------
#--- Setup compiler settings ---
#------------------------------------------------
# Set C language standard
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF) # Only standard features, no compiler-specific extensions
# Set C++ language standard
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) # Only standard features, no compiler-specific extensions
# Options
option(APP_WIN32 "Build app as a Windows GUI application" OFF)
# Set compiler flags based on build type
set(GENERAL_C_FLAGS
/MP # Enable multi-processor compilation
)
set(GENERAL_CXX_FLAGS
/MP # Enable multi-processor compilation
/EHsc # Enable C++ exception handling
)
set(DEBUG_C_FLAGS
/Od # Disable optimization for debugging
/Zi # Generate complete debugging information
/RTC1 # Enable runtime error checks
/Ob0 # Disable inline expansion
/W3 # Enable standard warnings
# /W4 # Enable high-level warnings
)
set(DEBUG_CXX_FLAGS
/Od # Disable optimization for debugging
/Zi # Generate complete debugging information
/RTC1 # Enable runtime error checks
/Ob0 # Disable inline expansion
/W3 # Enable standard warnings
# /W4 # Enable high-level warnings
)
set(RELEASE_C_FLAGS
/O2 # Optimize for speed
/Ot # Favor fast code
/Oi # Generate intrinsic functions
/GL # Enable whole program optimization
/Ob2 # Enable inline expansion
/W3 # Enable standard warnings
# /W4 # Enable high-level warnings
)
set(RELEASE_CXX_FLAGS
/O2 # Optimize for speed
/Ot # Favor fast code
/Oi # Generate intrinsic functions
/GL # Enable whole program optimization
/Ob2 # Enable inline expansion
/W3 # Enable standard warnings
# /W4 # Enable high-level warnings
)
#------------------------------------------------
#--- Project configuration ---
#------------------------------------------------
# Define the project settings
project("projectName")
# Source directory
set(SRC_DIR
"src"
)
# Resource files (add resource files here if needed)
set(RESOURCE_FILES
# "src/file.rc"
)
#------------------------------------------------
#--- Sanity checks ---
#------------------------------------------------
# Ensure build type is set (Debug, Release, etc.)
if(NOT CMAKE_BUILD_TYPE)
message(FATAL_ERROR "CMAKE_BUILD_TYPE must be set")
endif()
# Ensure DEV_LIB environment variable is defined (used for external libraries)
if(NOT DEFINED ENV{DEV_LIB})
message(FATAL_ERROR "DEV_LIB environment variable must be defined")
endif()
set(DEV_LIB $ENV{DEV_LIB})
#------------------------------------------------
#--- Include directories ---
#------------------------------------------------
# General include directories (add your common include paths here)
set(GENERAL_INCLUDE_DIRS
"src"
"sdi_toolBox_1.0.x/toolBox"
"${DEV_LIB}/boost_1_87_0"
"${DEV_LIB}/wxWidgets-3.2.8/include/msvc"
"${DEV_LIB}/wxWidgets-3.2.8/include"
"${DEV_LIB}/openssl-3.5.0/include"
)
# Additional include directories for Debug configuration
set(DEBUG_INCLUDE_DIRS
# "path/to/debug/include"
)
# Additional include directories for Release configuration
set(RELEASE_INCLUDE_DIRS
# "path/to/debug/include"
)
#------------------------------------------------
#--- Library directories ---
#------------------------------------------------
# General library directories (add your common library paths here)
set(GENERAL_LIBRARY_DIRS
${CMAKE_BINARY_DIR}
"${DEV_LIB}/boost_1_87_0/stage/lib"
"${DEV_LIB}/wxWidgets-3.2.8/lib/vc_x64_lib"
"${DEV_LIB}/openssl-3.5.0/lib"
)
# Additional library directories for Debug configuration
set(DEBUG_LIBRARY_DIRS
# "path/to/debug/lib"
)
# Additional library directories for Release configuration
set(RELEASE_LIBRARY_DIRS
# "path/to/debug/lib"
)
#------------------------------------------------
#--- Preprocessor definitions ---
#------------------------------------------------
# General preprocessor definitions (add your common defines here)
set(GENERAL_PREPROCESSOR_DEFINITIONS
"_CRT_SECURE_NO_DEPRECATE"
"_CRT_NONSTDC_NO_DEPRECATE"
"_UNICODE"
"_WINDOWS"
"NOMINMAX"
"UNICODE"
"WIN32"
"WIN32_LEAN_AND_MEAN"
)
# Additional preprocessor definitions for Debug configuration
set(DEBUG_PREPROCESSOR_DEFINITIONS
"_DEBUG"
"DEBUG"
"WXDEBUG"
)
# Additional preprocessor definitions for Release configuration
set(RELEASE_PREPROCESSOR_DEFINITIONS
"NDEBUG"
)
#------------------------------------------------
#--- Libraries and DLLs ---
#------------------------------------------------
# General libraries to link (add your common libraries here)
set(GENERAL_LIB
# "libssl.lib"
)
# Additional libraries for Debug configuration
set(DEBUG_LIB
# "libDebug.lib"
)
# Additional libraries for Release configuration
set(RELEASE_LIB
# "libRelease.lib"
)
# General DLLs to copy after build (add your common DLLs here)
set(GENERAL_BIN
# "path/to/dll"
)
# Additional DLLs for Debug configuration
set(DEBUG_BIN
# "path/to/debug/dll"
)
# Additional DLLs for Release configuration
set(RELEASE_BIN
# "path/to/release/dll"
)
#------------------------------------------------
#--- MSVC Debug Information Format Policy ---
#------------------------------------------------
# Ensure CMake policy CMP0141 is set to NEW to control the MSVC debug information format.
# This sets CMAKE_MSVC_DEBUG_INFORMATION_FORMAT to "EditAndContinue" for Debug and RelWithDebInfo configurations,
# and to "ProgramDatabase" for other configurations, but only when using the MSVC compiler.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()
#------------------------------------------------
#--- Source files gathering ---
#------------------------------------------------
# Collect all C and C++ source files recursively
file(GLOB_RECURSE SOURCES_C "${SRC_DIR}/*.c")
file(GLOB_RECURSE SOURCES_CPP "${SRC_DIR}/*.cpp")
#------------------------------------------------
#--- Target definition ---
#------------------------------------------------
# Define the main executable target
if(APP_WIN32)
add_executable(${PROJECT_NAME} WIN32
${SOURCES_C}
${SOURCES_CPP}
${RESOURCE_FILES}
)
else()
add_executable(${PROJECT_NAME}
${SOURCES_C}
${SOURCES_CPP}
${RESOURCE_FILES}
)
endif()
#------------------------------------------------
#--- Target properties setup ---
#------------------------------------------------
# Setup include directories
target_include_directories(${PROJECT_NAME} PRIVATE
${GENERAL_INCLUDE_DIRS}
$<$<CONFIG:Debug>:${DEBUG_INCLUDE_DIRS}>
$<$<CONFIG:Release>:${RELEASE_INCLUDE_DIRS}>
)
# Setup library directories
target_link_directories(${PROJECT_NAME} PRIVATE
${GENERAL_LIBRARY_DIRS}
$<$<CONFIG:Debug>:${DEBUG_LIBRARY_DIRS}>
$<$<CONFIG:Release>:${RELEASE_LIBRARY_DIRS}>
)
# Setup preprocessor definitions
target_compile_definitions(${PROJECT_NAME} PRIVATE
${GENERAL_PREPROCESSOR_DEFINITIONS}
$<$<CONFIG:Debug>:${DEBUG_PREPROCESSOR_DEFINITIONS}>
$<$<CONFIG:Release>:${RELEASE_PREPROCESSOR_DEFINITIONS}>
)
# Setup linked libraries
target_link_libraries(${PROJECT_NAME} PRIVATE
${GENERAL_LIB}
$<$<CONFIG:Debug>:${DEBUG_LIB}>
$<$<CONFIG:Release>:${RELEASE_LIB}>
)
# Setup compiler options
set(PROJET_C_FLAGS
${GENERAL_C_FLAGS}
$<$<CONFIG:Debug>:${DEBUG_C_FLAGS}>
$<$<CONFIG:Release>:${RELEASE_C_FLAGS}>
)
set(PROJET_CXX_FLAGS
${GENERAL_CXX_FLAGS}
$<$<CONFIG:Debug>:${DEBUG_CXX_FLAGS}>
$<$<CONFIG:Release>:${RELEASE_CXX_FLAGS}>
)
target_compile_options(${PROJECT_NAME} PRIVATE
$<$<COMPILE_LANGUAGE:C>:${PROJET_C_FLAGS}> # Apply to C files
$<$<COMPILE_LANGUAGE:CXX>:${PROJET_CXX_FLAGS}> # Apply to C++ files
)
# Setup compiler and linker options (MSVC specific)
if(MSVC)
target_link_options(${PROJECT_NAME} PRIVATE "/ignore:4099" /PROFILE)
endif()
#------------------------------------------------
#--- Post-build: Copy DLLs ---
#------------------------------------------------
# Copy DLLs to the output directory after build
if(GENERAL_BIN OR DEBUG_BIN OR RELEASE_BIN)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${GENERAL_BIN}
$<$<CONFIG:Debug>:${DEBUG_BIN}>
$<$<CONFIG:Release>:${RELEASE_BIN}>
${CMAKE_BINARY_DIR}
)
endif()
#------------------------------------------------
#--- Project compilation log ---
#------------------------------------------------
# Print project and environment information for diagnostics
message(STATUS "-------------------------------------------------------")
message(STATUS "┏(-_-)┛┗(-_-)┓┗(-_-)┛┏(-_-)┓┏(-_-)┓┗(-_-)┛┏(-_-)┛┗(-_-)┓")
message(STATUS "-------------------------------------------------------")
message(STATUS "--- Platform Information ------------------------------")
message(STATUS "System Name: ${CMAKE_SYSTEM_NAME}")
message(STATUS "Processor: ${CMAKE_SYSTEM_PROCESSOR}")
message(STATUS "CMake Generator: ${CMAKE_GENERATOR}")
message(STATUS "-------------------------------------------------------")
message(STATUS "--- Compiler/Language Settings ------------------------")
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C Standard: ${CMAKE_C_STANDARD}")
message(STATUS "C Compiler ID: ${CMAKE_C_COMPILER_ID}")
message(STATUS "C Compiler Version: ${CMAKE_C_COMPILER_VERSION}")
message(STATUS "C Common Compiler Flags: ${GENERAL_C_FLAGS}")
message(STATUS "C Debug Compiler Flags: ${DEBUG_C_FLAGS}")
message(STATUS "C Release Compiler Flags: ${RELEASE_C_FLAGS}")
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "C++ Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "C++ Common Compiler Flags: ${GENERAL_CXX_FLAGS}")
message(STATUS "C++ Debug Compiler Flags: ${DEBUG_CXX_FLAGS}")
message(STATUS "C++ Release Compiler Flags: ${RELEASE_CXX_FLAGS}")
message(STATUS "Common Linker Flags: ${CMAKE_EXE_LINKER_FLAGS}")
message(STATUS "Debug Linker Flags: ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
message(STATUS "Release Linker Flags: ${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
message(STATUS "-------------------------------------------------------")
message(STATUS "--- Path Information ----------------------------------")
message(STATUS "Source Dir: ${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "Binary Dir: ${CMAKE_CURRENT_BINARY_DIR}")
if(MSVC)
message(STATUS "-------------------------------------------------------")
message(STATUS "--- Microsoft Visual C++ (MSVC) Information -----------")
# Check the major compiler version
if(MSVC_VERSION GREATER_EQUAL 1950)
message(STATUS "MSVC Compiler Version: ${MSVC_VERSION} (Visual Studio 2026 or newer)")
elseif(MSVC_VERSION GREATER_EQUAL 1930)
message(STATUS "MSVC Compiler Version: ${MSVC_VERSION} (Visual Studio 2022)")
endif()
message(STATUS "MSVC Toolset Version: ${MSVC_TOOLSET_VERSION}")
endif()
message(STATUS "-------------------------------------------------------")
message(STATUS "--- Project Information -------------------------------")
message(STATUS "Project Name: ${PROJECT_NAME}")
message(STATUS "Preset Name: ${PRESET_NAME}")
message(STATUS "-------------------------------------------------------")
message(STATUS "┏(-_-)┛┗(-_-)┓┗(-_-)┛┏(-_-)┓┏(-_-)┓┗(-_-)┛┏(-_-)┛┗(-_-)┓")
message(STATUS "-------------------------------------------------------")