文章目录
- 1. 总体认识
- 2. CMake的使用
-
- 2.1. 终端命令
- 2.2. 创建最基本的CMake项目
- 2.3. 添加库
- 2.4. 添加库的使用要求
- 2.5. 添加生成器表达式
- 2.6. 安装和测试
1. 总体认识
- CMake是一个管理源代码构建的工具。最初,CMake是作为Makefile的生成器设计的,现在CMake可以用于生成现代构建系统(如Ninja)以及IDE(如Visual Studio和Xcode)的项目文件。
- CMake广泛用于C和C++语言,但也可以用于构建其他语言的源代码。
- CMake的安装:去CMake官网下载安装程序安装即可。
2. CMake的使用
2.1. 终端命令
mkdir build
cd build
cmake ..
cmake --build .
cd Debug
2.2. 创建最基本的CMake项目
- CMakeLists.txt
- tutorial.cxx:源文件
- TutorialConfig.h.in:配置文件
cmake_minimum_required(VERSION 3.10)
project(Tutorial VERSION 1.0)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
configure_file(TutorialConfig.h.in TutorialConfig.h)
add_executable(Tutorial tutorial.cxx)
target_include_directories(Tutorial PUBLIC ${PROJECT_BINARY_DIR})
// Tutorial的配置选项
2.3. 添加库
- MathFunctions:库目录
-- CMakeLists.txt
-- MathFunctions.cxx:主源文件(通过条件编译选项USE_MYMATH选择哪种实现)
-- MathFunctions.h:主头文件
-- mysqrt.cxx:自定义sqrt实现源文件
-- mysqrt.h:自定义sqrt实现头文件
- CMakeLists.txt
- tutorial.cxx:源文件
- TutorialConfig.h.in:配置文件
mkdir build
cd build
cmake .. [-DUSE_MYMATH=ON/OFF]
cmake --build .
cd Debug
cmake_minimum_required(VERSION 3.10)
project(Tutorial VERSION 1.0)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
configure_file(TutorialConfig.h.in TutorialConfig.h)
add_subdirectory(MathFunctions)
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC MathFunctions)
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
"${PROJECT_SOURCE_DIR}/MathFunctions"
)
add_library(MathFunctions MathFunctions.cxx)
option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
add_library(SqrtLibrary STATIC
mysqrt.cxx
)
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()
2.4. 添加库的使用要求
- MathFunctions:库目录
-- CMakeLists.txt
-- MathFunctions.cxx:主源文件(通过条件编译选项USE_MYMATH选择哪种实现)
-- MathFunctions.h:主头文件
-- mysqrt.cxx:自定义sqrt实现源文件
-- mysqrt.h:自定义sqrt实现头文件
- CMakeLists.txt
- tutorial.cxx:源文件
- TutorialConfig.h.in:配置文件
cmake_minimum_required(VERSION 3.10)
project(Tutorial VERSION 1.0)
add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
configure_file(TutorialConfig.h.in TutorialConfig.h)
add_subdirectory(MathFunctions)
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
add_library(MathFunctions MathFunctions.cxx)
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
add_library(SqrtLibrary STATIC
mysqrt.cxx
)
target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()
target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
2.5. 添加生成器表达式
- MathFunctions:库目录
-- CMakeLists.txt
-- MathFunctions.cxx:主源文件(通过条件编译选项USE_MYMATH选择哪种实现)
-- MathFunctions.h:主头文件
-- mysqrt.cxx:自定义sqrt实现源文件
-- mysqrt.h:自定义sqrt实现头文件
- CMakeLists.txt
- tutorial.cxx:源文件
- TutorialConfig.h.in:配置文件
cmake_minimum_required(VERSION 3.15)
project(Tutorial VERSION 1.0)
add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
set(gcc_like_cxx "$")
set(msvc_cxx "$")
target_compile_options(tutorial_compiler_flags INTERFACE
"$<${gcc_like_cxx}:$>"
"$<${msvc_cxx}:$>"
)
configure_file(TutorialConfig.h.in TutorialConfig.h)
add_subdirectory(MathFunctions)
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
add_library(MathFunctions MathFunctions.cxx)
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
add_library(SqrtLibrary STATIC
mysqrt.cxx
)
target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()
target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)
2.6. 安装和测试