# cmakeTest **Repository Path**: longlongint/cmake-test ## Basic Information - **Project Name**: cmakeTest - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-08-15 - **Last Updated**: 2021-08-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README CMake-Demo ===== [CMake 入门实战](https://hahack.com/codes/cmake) 的源代码。 copy from [github](https://github.com/wzpan/cmake-demo) > 个人理解,cmake 就是对 makefile 语句的一些封装 # 使用 ## windows > 假设已经安装 MinGW ```sh mkdir build && cd build cmake ..\Demo4 -G "MinGW Makefiles" mingw32-make ``` # Demo 解释 ## demo 1 ### [cmake_minimum_required][] 指定cmake的最低版本 ```cmake # 版本最低要求2.8 cmake_minimum_required (VERSION 2.8...3.21) ``` ### [project][] 设置工程名 ```cmake # 设置工程名叫 Demo1 project (Demo1) ``` ### [add_executable][] 使用指定源文件 `创建` 一个可执行文件 ```cmake #用 main.cc 创建可执行文件 a.exe add_executable(a.exe main.cc) ``` --- ## demo 2 ### [aux_source_directory][] 收集指定目录中的所有源文件并存储到列表 variable
疑问:哪些扩展名算源文件 ```cmake # 查找当前目录下的所有源文件,并将名称保存到 DIR_SRCS 变量 aux_source_directory(. DIR_SRCS) # 用 DIR_SRCS 保存的名字 创建 可执行文件 add_executable(a.exe ${DIR_SRCS}) ``` --- ## demo 3 ### [add_subdirectory][] 添加一个需要被构建的子目录 ```cmake # math 目录需要先被构建 add_subdirectory(math) ``` ### [target_link_libraries][] 给要链接的目标指定 `库或编译选项` ```cmake # 编译 a.exe 的时候需要链接 MathFunctions 库 target_link_libraries(a.exe MathFunctions) ``` ### [add_library][] 使用指定源文件 `创建` 一个库 ```cmake # 创建 MathFunctions 动态库, 默认静态库 add_library (MathFunctions SHARED MathFunctions.cc MathFunctions.h ) ``` --- ## demo 4 CMake 允许为项目增加选项,从而可根据需求选择合适的编译方案 ### [configure_file][] ```cmake # 加入一个配置头文件,会将 config.h.in 复制到 config.h # #cmakedefine VAR ... 被复制为 #define VAR ... configure_file ( "${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h" ) ``` ### [option][] 给用户提供一个选项 ```cmake # 是否使用自己的 MathFunctions 库,默认开 option (USE_MYMATH "Use provided math implementation" ON) ``` ### [set][] ### [include_directories][] 增加一个头文件搜索路径 ```cmake # 是否加入 MathFunctions 库 if (USE_MYMATH) include_directories ("${PROJECT_SOURCE_DIR}/math") add_subdirectory (math) set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions) endif (USE_MYMATH) ``` 生成命令 ```sh cmake ../Demo4 -DUSE_MYMATH=OFF ``` 当 CMakeList.txt 与 config.h.in 都含有 变量 USE_MYMATH 的定义 | CMakeList.txt | config.h.in | config.h | |--|--|--| OFF|ON/OFF|#undef ON|ON/OFF|#defined 所以,[configure_file][] 是为了让普通文件,也能使用CMake中的变量 --- ## demo 5 ### [install][] 指定安装规则 ```cmake # 指定二进制安装路径 install (TARGETS Demo DESTINATION bin) # 头文件安装路径 install (FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include) # 指定 MathFunctions 库的安装路径 install (TARGETS ${PROJECT_NAME}_MathFunctions DESTINATION lib) # 库头文件安装路径 install (FILES MathFunctions.h DESTINATION include) ``` 用法: ```sh mkdir build && cd build cmake ../Demo5 -DCMAKE_INSTALL_PREFIX=`pwd`/install make install -j16 ``` ### [enable_testing()][] 为当前目录和子目录启用测试 * [add_test][] + 添加测试项 * [set_tests_properties][] + 设置测试结果 --- ## demo 6 ### [include][] 从文件或模块加载 CMake 代码 ### [CheckFunctionExists][] 链接时检查函数是否存在 ```cmake # 检查系统是否支持 pow 函数 include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake) check_function_exists (pow HAVE_POW) ``` --- ## demo 7 设置版本信息 * 1、cmake 中加入: ```cmake set (Demo_VERSION_MAJOR 1) set (Demo_VERSION_MINOR 0) ``` * 2、config.h.in 中 ```h #define Demo_VERSION_MAJOR @Demo_VERSION_MAJOR@ #define Demo_VERSION_MINOR @Demo_VERSION_MINOR@ ``` --- ## demo 8 构建 cpack 包 ```cmake # 构建一个 CPack 安装包 include (InstallRequiredSystemLibraries) set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt") include (CPack) ``` 用法: ```sh mkdir build && cd build cmake ../Demo5 -DCMAKE_INSTALL_PREFIX=`pwd`/install make install -j16 # 生成二进制安装包 cpack -C CPackConfig.cmake # 生成源码安装包 cpack -C CPackSourceConfig.cmake # 安装 sh Demo8-1.0.1-Linux.sh ``` [cmake_minimum_required]:https://cmake.org/cmake/help/latest/command/cmake_minimum_required.html [project]:https://cmake.org/cmake/help/latest/command/project.html [add_executable]:https://cmake.org/cmake/help/latest/command/add_executable.html [aux_source_directory]:https://cmake.org/cmake/help/latest/command/aux_source_directory.html [add_subdirectory]:https://cmake.org/cmake/help/latest/command/add_subdirectory.html [target_link_libraries]:https://cmake.org/cmake/help/latest/command/target_link_libraries.html [add_library]:https://cmake.org/cmake/help/latest/command/add_library.html [configure_file]:https://cmake.org/cmake/help/latest/command/configure_file.html [option]:https://cmake.org/cmake/help/latest/command/option.html [include_directories]:https://cmake.org/cmake/help/latest/command/include_directories.html [set]:https://cmake.org/cmake/help/latest/command/set.html [install]:https://cmake.org/cmake/help/latest/command/install.html [enable_testing()]:https://cmake.org/cmake/help/latest/command/enable_testing.html [add_test]:https://cmake.org/cmake/help/latest/command/add_test.html [set_tests_properties]:https://cmake.org/cmake/help/latest/command/set_tests_properties.html [CheckFunctionExists]:https://cmake.org/cmake/help/latest/module/CheckFunctionExists.html [include]:https://cmake.org/cmake/help/latest/command/include.html