内容纲要

目标

设置变量 set
通用预定义 add_definitions
目标 add_library
add_executable
目标宏 target_compile_definitions
目标 include dir target_include_directories
目标 link dir target_link_directories
目标 link lib target_link_libraries
自定义命令行 add_custom_command

简单例子

A-hello-cmake

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)

# Set the project name
project (hello_cmake)

# Add an executable
add_executable(hello_cmake main.cpp)

B-hello-headers

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)

# Set the project name
project (hello_headers)

# Create a sources variable with a link to all cpp files to compile
set(SOURCES
    src/Hello.cpp
    src/main.cpp
)

# Add an executable with the above sources
add_executable(hello_headers ${SOURCES})

# Set the directories that should be included in the build command for this target
# when running g++ these will be included as -I/directory/path/
target_include_directories(hello_headers
    PRIVATE 
        ${PROJECT_SOURCE_DIR}/include
)

C-static-library

cmake_minimum_required(VERSION 3.5)

project(hello_library)

############################################################
# Create a library
############################################################

#Generate the static library from the library sources
add_library(hello_library STATIC 
    src/Hello.cpp
)

target_include_directories(hello_library
    PUBLIC 
        ${PROJECT_SOURCE_DIR}/include
)

############################################################
# Create an executable
############################################################

# Add an executable with the above sources
add_executable(hello_binary 
    src/main.cpp
)

# link the new hello_library target with the hello_binary target
target_link_libraries( hello_binary
    PRIVATE 
        hello_library
)

D-shared-library

cmake_minimum_required(VERSION 3.5)

project(hello_library)

############################################################
# Create a library
############################################################

#Generate the shared library from the library sources
add_library(hello_library SHARED 
    src/Hello.cpp
)
add_library(hello::library ALIAS hello_library)

target_include_directories(hello_library
    PUBLIC 
        ${PROJECT_SOURCE_DIR}/include
)

############################################################
# Create an executable
############################################################

# Add an executable with the above sources
add_executable(hello_binary
    src/main.cpp
)

# link the new hello_library target with the hello_binary target
target_link_libraries( hello_binary
    PRIVATE 
        hello::library
)

E-installing

cmake_minimum_required(VERSION 3.5)

project(cmake_examples_install)

############################################################
# Create a library
############################################################

#Generate the shared library from the library sources
add_library(cmake_examples_inst SHARED
    src/Hello.cpp
)

target_include_directories(cmake_examples_inst
    PUBLIC 
        ${PROJECT_SOURCE_DIR}/include
)

############################################################
# Create an executable
############################################################

# Add an executable with the above sources
add_executable(cmake_examples_inst_bin
    src/main.cpp
)

# link the new hello_library target with the hello_binary target
target_link_libraries( cmake_examples_inst_bin
    PRIVATE 
        cmake_examples_inst
)

############################################################
# Install
############################################################

# Binaries
install (TARGETS cmake_examples_inst_bin
    DESTINATION bin)

# Library
# Note: may not work on windows
install (TARGETS cmake_examples_inst
    LIBRARY DESTINATION lib)

# Header files
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ 
    DESTINATION include)

# Config
install (FILES cmake-examples.conf
    DESTINATION etc)

添加 visual studio 筛选器

set(TMP_SRC 
    "${SRCROOT}/zhw/debug_console.h"
    "${SRCROOT}/zhw/debug_console.cpp"
)
source_group("debug_console" FILES ${TMP_SRC})
list(APPEND SRC ${TMP_SRC})

添加文件

  • file(GLOB LIBS "${BOOST_PATH}/libboost*.a")
file(GLOB_RECURSE SRC_FILES 
    ../Source/*.h
    ../Source/*.cpp
    Source/*.cpp
    Source/*.h
)

排除文件

  FILE( GLOB HARUNA_EXE_EXAMPLE_SRC_FILES
    "${CUR_ROOT}/*.*" )
  SET( HARUNA_EXE_SUPPORT_SRC_FILES
    "${CUR_ROOT}/main.cpp"
    "${CUR_ROOT}/demo.cpp"
    "${CUR_ROOT}/demo.h"
    "${CUR_ROOT}/exe_stdafx.h"
    "${CUR_ROOT}/exe_stdafx.cpp")
  LIST( REMOVE_ITEM HARUNA_EXE_EXAMPLE_SRC_FILES ${HARUNA_EXE_SUPPORT_SRC_FILES} )

替换字符串

  • STRING(REGEX REPLACE "VALUE_ROOT_PATH" "${BUILD_PATH}" MOD_IO_UTILS "${IO_UTILS}" )

cmake 模块

发表评论

电子邮件地址不会被公开。 必填项已用*标注