在这个部分,我们将说明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)