CMake 学习【九】—— 融合动态库和静态库

在这个部分,我们将说明BUILD_SHARED_LIBS可以被用来控制add_library()的默认行为,允许控制创建没有类型声明的库(STATIC, SHARED, MODULE or OBJECT)是怎样被创建的。
为了完成这项的设置,我们需要按照下面的步骤进行设置:

1. 在顶层的CMakeLists.txt中添加BUILD_SHARED_LIBS

为了使用户能够选择,我们使用option()

option(BUILD_SHARED_LIBS "Build using shared libraries" ON)

然后我们修改MathFunctions,让它变成了一个使用了mysqrt或sqrt库的封装,而不再是由调用者决定使用哪个函数。
现在顶层的CMakeLists.txt中的内容是这样的:

cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# 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)

2. 设置MathFunctions/CMakeLists.txt

现在我们在顶层的cmake中设置了始终使用这个mathfunction的库,接下来我们就需要更新库本身的逻辑。
所以我们需要在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)

  # 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(MathFunctions PRIVATE SqrtLibrary)
endif()

# define the symbol stating we are using the declspec(dllexport) when
# building on windows
target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH")

# install rules
install(TARGETS MathFunctions DESTINATION lib)
install(FILES MathFunctions.h DESTINATION include)

3. 更新MathFunctions/mysqrt.cxx

在这个文件中使用mathfunctions and 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;
}
}
}

4. 更改tutorial.cxx

在该文件中不再使用USE_MYMATH

Always include MathFunctions.h

Always use mathfunctions::sqrt

Don’t include cmath

5. 更新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}
                        )

  target_link_libraries(MathFunctions PRIVATE SqrtLibrary)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 CMake 编译动态库静态库,你可以按照以下步骤进行操作: 1. 创建一个 CMakeLists.txt 文件,该文件描述了构建过程和项目配置。 2. 在 CMakeLists.txt 文件中,使用 `add_library` 命令来定义要构建的库。使用 `SHARED` 参数来指定动态库,使用 `STATIC` 参数来指定静态库。例如: ```cmake # 构建动态库 add_library(mylib SHARED src/mylib.cpp) # 构建静态库 add_library(mylib_static STATIC src/mylib.cpp) ``` 3. 如果你有多个源文件,可以将它们一起添加到库中: ```cmake # 构建动态库 add_library(mylib SHARED src/mylib.cpp src/other.cpp) # 构建静态库 add_library(mylib_static STATIC src/mylib.cpp src/other.cpp) ``` 4. 如果你想为库定义公共头文件目录,可以使用 `target_include_directories` 命令。例如: ```cmake target_include_directories(mylib PUBLIC include) ``` 5. 如果你希望链接其他库到你的库中,可以使用 `target_link_libraries` 命令。例如: ```cmake target_link_libraries(mylib PUBLIC otherlib) ``` 6. 最后,在 CMakeLists.txt 文件中添加一个 `install` 命令,以便在构建过程完成后安装库文件到指定的目录。例如: ```cmake install(TARGETS mylib DESTINATION lib) install(FILES include/mylib.h DESTINATION include) ``` 7. 运行 CMake 命令来生成构建系统所需的文件: ```shell cmake . ``` 8. 使用生成的构建系统来编译和构建库: ```shell make ``` 在构建过程完成后,你将在指定的目录(例如 `lib`)中找到生成的动态库静态库文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值