这个问题的主要目的是编写CMakeLists.txt生成一个动态库,“包含/链接”一个静态库。
让我创建场景:
我写这个脚本的尝试是在下面的行:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror -m64 -fPIC ")
add_executable(myapp mycode.cpp)
target_link_libraries(myapp thirdparty)
add_library(mylib SHARED myapp)
但是当然这是行不通的,我需要一些帮助来正确地编写它
答案 0 :(得分:1)
现在,让我们删除myapp
并仅关注您要创建的库。
也就是说,这就是你可以做的
cmake_minimum_required(VERSION 3.12)
project(AwesomeLib)
include(GenerateExportHeader)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
find_package(ThirdParty REQUIRED)
add_library(mylib SHARED mylib.cpp)
target_link_libraries(mylib PUBLIC ThirdParty)
# Note: If you library will be cross-platform, flag should be conditionally specified. Have a look at generator-expression
target_compile_options(mylib PRIVATE -Wall -Werror)
generate_export_header(mylib)
# TODO:
# * add install rules
# * generate config-file package
# * add tests
注意:
generate_export_header
将使用MYLIB_EXPORT宏生成“ mylib_export.h”标头,以导出符号。参见https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html find_package(ThirdParty REQUIRED)
背后的想法,建议您阅读config-file程序包。参见Correct way to use third-party libraries in cmake project