C++ – CMakeLists.txt 简单示例

PS:由于 WP-Editor.md 的 Bug ,脚本中的 $ 符号不显示,需手动添加。

解决方案

cmake_minimum_required (VERSION 3.8)

# Main project
project ("Redcraft")

# 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")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
    add_compile_definitions ("PLATFORM_LINUX")
else ()
    add_compile_definitions ("PLATFORM_UNKNOWN")
endif ()

# Add subproject
add_subdirectory ("Redcraft.Core")
add_subdirectory ("Redcraft.Debug")

LIB & DLL 项目

cmake_minimum_required (VERSION 3.8)

# Set module name.
set (MODULE_NAME "Redcraft.Core")

# Add target.
file (GLOB_RECURSE MODULE_SOURCE_FILES "Source/*")
add_library ({MODULE_NAME}{MODULE_SOURCE_FILES})

# 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.
set (MODULE_NAME "Redcraft.Debug")

# Add target.
file (GLOB_RECURSE MODULE_SOURCE_FILES "Source/*")
add_executable({MODULE_NAME}{MODULE_SOURCE_FILES})

# 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)

发表评论