CMake官方教程练习记录

CMake官方教程练习记录

官方教程地址 官方代码地址

1. 原本的编译 1.1 工程的编译方式

文件目录

[root@VM-4-6-centos staticLib]# tree ..|– include| `– swap.h|– main|– main.cpp`– src `– swap.cpp2 directories, 4 files

swap.cpp

void swap(int *a, int *b) { int t = 0; t = *a; *a = *b; *b = t;}

swap.h

void swap(int *a, int *b);

main.cpp

#include #include #include “swap.h”int main() { int a = 1, b = 2; std::cout EXTRA_LIBS})target_include_directories(Tutorial PUBLIC “${PROJECT_BINARY_DIR}” ${EXTRA_INCLUDES} )

#cmakedefine 用于h.in文件中,只有当CMakeLists.txt中的同名变量为真时才会在生成的头文件中定义,区别于#define无论何时都会定义。这个定义是为了编译时 h.in生成的h文件中带有定义,使得cpp中可以判断导入哪个头文件、使用哪些函数等。

额外问题

如果Configure文件配置语句在OPTION之前会有什么问题?

那么配置文件的部分值就只能通过自己的输入,获取不了option的默认值。

STEP 3

为库添加使用要求

target_compile_definitions()target_compile_options()target_include_directories()target_link_directories()target_link_options()target_precompile_headers()target_sources()

这一节让顶层list文件可以不用再包含子目录的头文件。

MathFunctions/CMakelists.txt

add_library(MathFunctions mysqrt.cxx)# TODO 1: State that anybody linking to MathFunctions needs to include the# current source directory, while MathFunctions itself doesn’t.# Hint: Use target_include_directories with the INTERFACE keywordtarget_include_directories(MathFunctions INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})message(STATUS “current source dir ${CMAKE_CURRENT_SOURCE_DIR}”)message(STATUS “current bin dir ${CMAKE_CURRENT_BINARY_DIR}”)message(STATUS “source dir ${CMAKE_SOURCE_DIR}”)message(STATUS “bin dir ${CMAKE_BINARY_DIR}”)

可以看一下这些参数都代表什么位置

— current source dir /home/maheWork/learnCmake/cmake-3.25.1-tutorial-source/Step3/MathFunctions– current bin dir /home/maheWork/learnCmake/cmake-3.25.1-tutorial-source/Step3_build/MathFunctions– source dir /home/maheWork/learnCmake/cmake-3.25.1-tutorial-source/Step3– bin dir /home/maheWork/learnCmake/cmake-3.25.1-tutorial-source/Step3_build

CURRENT 代表当前目录。

INTERFACE 的官网解释是 Remember INTERFACE means things that consumers require but the producer doesn’t,意味自己不需要这个头文件但是如果其他其他调用需要这个头文件。

STEP 4

使用接口库设置 C++ 标准,使用生成器表达式添加编译器警告标志

target_compile_features

先创建一个接口库用来设定信息 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)

target_compile_features()用来设置编译器版本

设置不同编译器的报错信息 set(gcc_like_cxx “$”)set(msvc_cxx “$”)target_compile_options(tutorial_compiler_flags INTERFACE “$” “$” ) 记得给方法链接上接口库 STEP 5 安装和测试

安装

在make完之后执行make install指令可以按照makefile的内容将可执行文件、头文件、库文件安装到某个地方,默认是usr/local为前缀,然后就可以直接在命令行通过文件名直接调用了

教程中出现三种情况:

install(TARGETS 库名 DESTINATION lib)install(FILES (头文件名)MathFunctions.h DESTINATION include)install(TARGETS (可执行文件名)Tutorial DESTINATION bin)

测试

在make完之后执行make test指令

# Enableenable_testing()# 测试名称和参数,以及成功的条件add_test(NAME Runs COMMAND Tutorial 25)set_tests_properties(Usage PROPERTIES PASS_REGULAR_EXPRESSION “Usage:.*number” )# 用函数的形式function(do_test target arg result) add_test(NAME Comp${arg} COMMAND ${target} ${arg}) set_tests_properties(Comp${arg} PROPERTIES PASS_REGULAR_EXPRESSION ${result} )endfunction()# 使用示例do_test(Tutorial 4 “4 is 2”)do_test(Tutorial 9 “9 is 3”)do_test(Tutorial 5 “5 is 2.236”)do_test(Tutorial 7 “7 is 2.645”)do_test(Tutorial 25 “25 is 5”)do_test(Tutorial -25 “-25 is (-nan|nan|0)”)do_test(Tutorial 0.0001 “0.0001 is 0.01”)# 以上都是写在CMakelists中的


比丘资源网 » CMake官方教程练习记录

发表回复

提供最优质的资源集合

立即查看 了解详情