CMake 中文简易手册
最基本的项目是从源代码文件构建的可执行文件。对于简单的项目,只需要三行代码。
cmake_minimum_required(VERSION 3.10)
# set the project name
project(Tutorial)
# add the executable
add_executable(Tutorial tutorial.cxx)
如果想要将子工程构建为动态链接库后用于本工程,需要进行一下操作:
以下面的文件树为例:
CMakeTutorial 文件夹下的 CMakeList.txt 内容如下:
# set the minimum required version of cmake for a project
cmake_minimum_required(VERSION 3.14)
# set the project name and version
project(CMakeTutorial VERSION 1.3.0.0)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# provide an option "USE_USER_TOOLS" for the user to select as True or False
set(USE_USER_TOOLS True)
# provide an option "USE_EXISTIONG_USER_TOOLS" for the user to select as True or False
set(USE_EXISTIONG_USER_TOOLS False)
if(${USE_USER_TOOLS})
if(${USE_EXISTIONG_USER_TOOLS})
message("Use author provided existing tools implementation")
# appends elements to the list "EXTRA_LIBS"
list(APPEND EXTRA_LIBS libtools)
# appends elements to the list "EXTRA_INCLUDES"
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/tools")
## copy library to the binary tree
file(COPY ${PROJECT_BINARY_DIR}/tools/libtools.dll DESTINATION ${PROJECT_SOURCE_DIR}/tools/)
else()
message("Use author provided tools implementation")
# add a subdirectory to the build
add_subdirectory(tools)
# appends elements to the list "EXTRA_LIBS"
list(APPEND EXTRA_LIBS tools)
# appends elements to the list "EXTRA_INCLUDES"
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/tools")
endif()
endif()
# configure a header file to pass some of the CMake settings to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
# find all source files in a directory.
aux_source_directory(. SRCS_LIST)
# add the executable
add_executable(CMakeTutorial ${SRCS_LIST})
# specify the paths in which the linker should search for libraries when linking a given target
target_link_directories(CMakeTutorial PUBLIC ${EXTRA_INCLUDES})
# link libraries to target
target_link_libraries(CMakeTutorial PUBLIC ${EXTRA_LIBS})
# specifies include directories to use when compiling a given target
# add the binary tree to the search path for include files
# so that we will find config.h and source
target_include_directories(CMakeTutorial PUBLIC
${PROJECT_SOURCE_DIR}
${PROJECT_BINARY_DIR}
${EXTRA_INCLUDES}
)
tools 文件夹下的 CMakeList.txt
aux_source_directory(. TOOL_LIB_SRCS_LIST)
add_library(tools SHARED ${TOOL_LIB_SRCS_LIST})
target_include_directories(tools
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
构建后结果
如果编译子目录工程之后,不需要在修改,那么可以将动态库连接语句改为:
# provide an option "USE_EXISTIONG_USER_TOOLS" for the user to select as True or False
set(USE_EXISTIONG_USER_TOOLS True)
任世事无常,勿忘初心
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析