CMake官方教程中文翻译 Step 10: Selecting Static or Shared Librarie

鉴于自己破烂的英语,所以把cmake的官方文档用 谷歌翻译 翻译下来方便查看。
英语好的同学建议直接去看cmake官方文档(英文)学习:地址 点这里
或复制:https://cmake.org/cmake/help/latest/guide/tutorial/index.html

因为官方文档有点多,所以只截取CMake Tutorial的 step1——step12 进行翻译,这是第10步的翻译,以下是每一步的翻译链接:
Documentation » CMake Tutorial » Step 1: A Basic Starting Point
Documentation » CMake Tutorial » Step 2: Adding a Library
Documentation » CMake Tutorial » Step 3: Adding Usage Requirements for a Library
Documentation » CMake Tutorial » Step 4: Adding Generator Expressions
Documentation » CMake Tutorial » Step 5: Installing and Testing
Documentation » CMake Tutorial » Step 6: Adding Support for a Testing Dashboard
Documentation » CMake Tutorial » Step 7: Adding System Introspection
Documentation » CMake Tutorial » Step 8: Adding a Custom Command and Generated File
Documentation » CMake Tutorial » Step 9: Packaging an Installer
[Documentation » CMake Tutorial » Step 10: Selecting Static or Shared Libraries]
Documentation » CMake Tutorial » Step 11: Adding Export Configuration
Documentation » CMake Tutorial » Step 12: Packaging Debug and Release

谷歌翻译可能有错,此文档的目的仅是加快观看英文官方文档的速度,所以请结合英文官方文档观看

Step 10: Selecting Static or Shared Libraries

在本节中,我们将展示如何使用 BUILD_SHARED_LIBS 变量来控制 add_library() 的默认行为,并允许控制如何构建没有显式类型(STATIC、SHARED、MODULE 或 OBJECT)的库。

为了实现这一点,我们需要将 BUILD_SHARED_LIBS 添加到顶级 CMakeLists.txt。 我们使用 option() 命令,因为它允许用户选择该值是 ON 还是 OFF。

CMakeLists.txt
option(BUILD_SHARED_LIBS “Build using shared libraries” ON)

接下来,我们需要为静态库和共享库指定输出目录。

CMakeLists.txt
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)

最后,更新 MathFunctions/MathFunctions.h 以使用 dll 导出定义:

MathFunctions/MathFunctions.h
#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。

MathFunctions/CMakeLists.txt

state that SqrtLibrary need PIC when the default is shared libraries

set_target_properties(SqrtLibrary PROPERTIES
POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
)

定义 EXPORTING_MYMATH 表明我们在 Windows 上构建时使用 declspec(dllexport)。

MathFunctions/CMakeLists.txt
# define the symbol stating we are using the declspec(dllexport) when
# building on windows
target_compile_definitions(MathFunctions PRIVATE “EXPORTING_MYMATH”)

练习:我们修改了 MathFunctions.h 以使用 dll 导出定义。 使用 CMake 文档,你能找到一个帮助模块来简化这个过程吗?

  • 18
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值