C++ – CMakeLists.txt 简单示例

PS:由于 WP-Editor.md 的 Bug ,脚本中的 $ 符号不显示,需手动添加。
PS:换成 WP Githuber MD 之后就没有这个 Bug 了。

解决方案

cmake_minimum_required (VERSION 3.8)

# Main project
string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR})
project (${CURRENT_FOLDER})
message (STATUS "Configuring project: " ${CURRENT_FOLDER})

# Reset the binary file directory
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Build")
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Build")
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Binaries")

# Configure compile options
set (BUILD_SHARED_LIBS true)
set (CMAKE_CXX_STANDARD 20)

# Define platform macros
add_compile_definitions ("PLATFORM_NAME=${CMAKE_SYSTEM_NAME}")
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
    add_compile_definitions ("PLATFORM_WINDOWS=1")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
    add_compile_definitions ("PLATFORM_LINUX=1")
else ()
    add_compile_definitions ("PLATFORM_UNKNOWN=1")
endif ()

# Define configuration type macros
add_compile_definitions ("BUILD_TYPE=${CMAKE_BUILD_TYPE}")
if (CMAKE_BUILD_TYPE MATCHES "Debug")
    add_compile_definitions ("BUILD_DEBUG=1")
elseif (CMAKE_BUILD_TYPE MATCHES "Release")
    add_compile_definitions ("BUILD_RELEASE=1")
else ()
    add_compile_definitions ("BUILD_UNKNOWN=1")
endif ()

# Add subproject  
file (GLOB PROJECT_FOLDERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/*")
foreach (PROJECT_SUBDIRECTORY ${PROJECT_FOLDERS}) 
    if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_SUBDIRECTORY}")
        file (GLOB PROJECT_CMAKELISTS "${PROJECT_SUBDIRECTORY}/CMakeLists.txt")
        if (NOT "${PROJECT_CMAKELISTS}" STREQUAL "")
            message (STATUS "Add subdirectory: " ${PROJECT_SUBDIRECTORY})
            add_subdirectory (${PROJECT_SUBDIRECTORY})
        endif ()
    endif ()
endforeach ()

LIB & DLL 项目

cmake_minimum_required (VERSION 3.8)

# Set module name.
string(REGEX REPLACE ".*/(.*)" "\\1" MODULE_NAME ${CMAKE_CURRENT_SOURCE_DIR})
message (STATUS "Configuring module: " ${MODULE_NAME})

# Add target.
file (GLOB_RECURSE MODULE_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Source/*")
add_library (${MODULE_NAME} ${MODULE_SOURCE_FILES})
target_compile_definitions (${MODULE_NAME} PRIVATE "MODULE_NAME=${MODULE_NAME}")

# Add include directories.
target_include_directories (${MODULE_NAME} PUBLIC "Source/Public")
target_include_directories (${MODULE_NAME} PRIVATE "Source/Private")

# Define API macro.
string (TOUPPER ${MODULE_NAME} MODULE_API)
string (REGEX REPLACE "[^A-Z ^0-1]" "" MODULE_API ${MODULE_API})
set (MODULE_API "${MODULE_API}_API")
target_compile_definitions (${MODULE_NAME} PRIVATE "${MODULE_API}=DLLEXPORT")
target_compile_definitions (${MODULE_NAME} INTERFACE "${MODULE_API}=DLLIMPORT")

# Add project dependencies
#target_link_libraries (${MODULE_NAME} PRIVATE Redcraft.Core)

EXE 项目

cmake_minimum_required (VERSION 3.8)

# Set module name.
string(REGEX REPLACE ".*/(.*)" "\\1" MODULE_NAME ${CMAKE_CURRENT_SOURCE_DIR})
message (STATUS "Configuring module: " ${MODULE_NAME})

# Add target.
file (GLOB_RECURSE MODULE_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Source/*")
add_executable(${MODULE_NAME} ${MODULE_SOURCE_FILES})
target_compile_definitions (${MODULE_NAME} PRIVATE "MODULE_NAME=${MODULE_NAME}")

# Add include directories.
target_include_directories (${MODULE_NAME} PRIVATE "Source")

# Define API macro.
string (TOUPPER ${MODULE_NAME} MODULE_API)
string (REGEX REPLACE "[^A-Z ^0-1]" "" MODULE_API ${MODULE_API})
set (MODULE_API "${MODULE_API}_API")
target_compile_definitions (${MODULE_NAME} PRIVATE "${MODULE_API}=DLLEXPORT")
target_compile_definitions (${MODULE_NAME} INTERFACE "${MODULE_API}=DLLIMPORT")

# Add project dependencies
target_link_libraries (${MODULE_NAME} PRIVATE Redcraft.Core)

发表回复