持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第30天,点击查看活动详情。 写文章不易,阅读之前请给我点个赞吧~
官方教程使用说明及总结
一、教程预览
本节内容可独立阅读,如果想从最基础的部分学起,请阅读上两篇文章CMake官方教程使用说明及重点总结上和CMake官方教程使用说明及重点总结中
我们将通过上中下讲述这个部分的内容,这里讲述下的部分
上主要讲述:
- 步骤1:一个基本的起点
- 练习1 -建立一个基本项目
- 练习2 -指定c++标准
- 练习3 -添加版本号和配置的头文件
中主要讲述:
- 步骤2:添加库
- 练习1 -创建库
- 练习2 -使我们的库可选
- 步骤3:添加库使用要求
- 练习1 -添加库的使用要求
- 步骤4:添加生成器表达式
- 练习1 -使用接口库设置c++标准
- 练习2 -用生成器表达式添加编译器警告标志
- 步骤5:安装和测试
- 练习1 -安装规则
- 练习2 -测试支持
下主要讲述:
- 步骤6:添加对测试仪表板的支持
- 步骤7:添加系统自检
- 步骤8:添加自定义命令和生成文件
- 步骤9:打包安装程序
- 步骤10:选择静态库或共享库
- 步骤11:添加导出配置
- 步骤12:打包、调试和发布
二、实战串讲
步骤6:添加对测试仪表板的支持
步骤7:添加系统自检
步骤8:添加自定义命令和生成文件
步骤9:打包安装程序
步骤10:选择静态库或共享库
这里我们重点看下 Step 10 ,这个部分展示了 BUILD_SHARED_LIBS 变量是如何来控制 add_library() 的默认行为的,并允许控制没有具体类型的库(STATIC、SHARED、MODULE或OBJECT)如何构建。
为此,我们需要将 BUILD_SHARED_LIBS 添加到顶层的 CMakeLists.txt 中。我们使用option()命令,因为它允许用户选择值是ON还是OFF。
接下来,我们将重构 MathFunctions,使其成为使用 mysqrt 或 sqrt 进行封装的真正库,而不是让调用代码执行此逻辑。这也意味着 USE_MYMATH 不会控制 MathFunctions 的构建,而是控制这个库的行为。
第一步是更新顶层CMakeLists.txt的开始部分,如下所示:
cmake_minimum_required(VERSION 3.15)
# set the project name and version
project(Tutorial VERSION 1.0)
# specify the C++ standard
add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
# add compiler warning flags just when building this project via
# the BUILD_INTERFACE genex
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
target_compile_options(tutorial_compiler_flags INTERFACE
"$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>>"
"$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
)
# control where the static and shared libraries are built so that on windows
# we don't need to tinker with the path to run the executable
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
# configure a header file to pass the version number only
configure_file(TutorialConfig.h.in TutorialConfig.h)
# add the MathFunctions library
add_subdirectory(MathFunctions)
# add the executable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
既然我们已经使 MathFunctions 始终被使用,我们将需要更新该库的逻辑。因此,在MathFunctions/CMakeLists.txt中,我们需要创建一个 SqrtLibrary,它将在 USE_MYMATH 启用时有条件地构建和安装。现在,由于这是一个教程,我们将显式地要求SqrtLibrary 是静态构建的。
最终的结果是 MathFunctions/CMakeLists.txt 应该是这样的:
# add the library that runs
add_library(MathFunctions MathFunctions.cxx)
# state that anybody linking to us needs to include the current source dir
# to find MathFunctions.h, while we don't.
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# first we add the executable that generates the table
add_executable(MakeTable MakeTable.cxx)
target_link_libraries(MakeTable PRIVATE tutorial_compiler_flags)
# add the command to generate the source code
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
DEPENDS MakeTable
)
# library that just does sqrt
add_library(SqrtLibrary STATIC
mysqrt.cxx
${CMAKE_CURRENT_BINARY_DIR}/Table.h
)
# state that we depend on our binary dir to find Table.h
target_include_directories(SqrtLibrary PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
)
target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()
target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)
# define the symbol stating we are using the declspec(dllexport) when
# building on windows
target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH")
# install libs
set(installable_libs MathFunctions tutorial_compiler_flags)
if(TARGET SqrtLibrary)
list(APPEND installable_libs SqrtLibrary)
endif()
install(TARGETS ${installable_libs} DESTINATION lib)
# install include headers
install(FILES MathFunctions.h DESTINATION include)
接下来, 更新 MathFunctions / mysqrt.CXX 使用mathfunctions 和 detail 名称空间:
#include <iostream>
#include "MathFunctions.h"
// include the generated table
#include "Table.h"
namespace mathfunctions {
namespace detail {
// a hack square root calculation using simple operations
double mysqrt(double x)
{
if (x <= 0) {
return 0;
}
// use the table to help find an initial value
double result = x;
if (x >= 1 && x < 10) {
std::cout << "Use the table to help find an initial value " << std::endl;
result = sqrtTable[static_cast<int>(x)];
}
// do ten iterations
for (int i = 0; i < 10; ++i) {
if (result <= 0) {
result = 0.1;
}
double delta = x - (result * result);
result = result + 0.5 * delta / result;
std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;
}
return result;
}
}
}
我们还需要在 tutorial.cxx 中做一些改变,使它不再使用 USE_MYMATH:
- 总是包括 MathFunctions.h
- 总是使用 mathfunctions::sqrt
- 不包括 cmath 最后,更新 MathFunctions/MathFunctions.h 以使用dll 导出定义:
#if defined(_WIN32)
# if defined(EXPORTING_MYMATH)
# define DECLSPEC __declspec(dllexport)
# else
# define DECLSPEC __declspec(dllimport)
# endif
#else // non windows
# define DECLSPEC
#endif
namespace mathfunctions {
double DECLSPEC sqrt(double x);
}
此时,如果您构建了所有内容,您可能会注意到链接失败,因为我们正在将一个没有位置无关代码的静态库与一个有位置无关代码的库组合在一起。解决方案是显式地将 SqrtLibrary 的 POSITION_INDEPENDENT_CODE 目标属性设置为 True,无论构建类型是什么。
# state that SqrtLibrary need PIC when the default is shared libraries
set_target_properties(SqrtLibrary PROPERTIES
POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
)