如何配置CMakeLists.txt以安装共享库的公共头?

问题描述:

我想使用cmake安装我的库 edv ,但是在执行时:

I want to use cmake to install my library edv but when I execute:

cmake --build . --target install

它会安装,但只会创建 bin / edv。 dll lib /<空> 。如何制作cmake在 include /...内安装 EDV_PUBLIC_INCLUDE_DIRECTORIES

It installs but it only creates the bin/edv.dll and lib/ < empty >. How can I make cmake to install the EDV_PUBLIC_INCLUDE_DIRECTORIES inside an include/... ?

这是我的 CMakeLists.txt

cmake_minimum_required(VERSION 3.12)

project(edv)

# include PUBLIC directories
set(EDV_PUBLIC_INCLUDE_DIRECTORIES      include/ )

set(EDV_PRIVATE_INCLUDE_DIRECTORIES     src/   )

# Edv source files list
file(GLOB_RECURSE EDV_SOURCE_FILES "src/*.cpp" "src/*.hpp*")


# build the library
add_library(${PROJECT_NAME} SHARED ${EDV_SOURCE_FILES} )

target_include_directories(${PROJECT_NAME} PUBLIC ${EDV_PUBLIC_INCLUDE_DIRECTORIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${EDV_PRIVATE_INCLUDE_DIRECTORIES})

install (TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    PUBLIC_HEADER DESTINATION include
)


CMake无法推断出要从中安装的头文件集目标。这是有道理的,因为目标可能同时包含私有和公共头文件,但是CMake不会区分它们。因此,您必须在 INSTALL(FILES ...)命令中明确列出头文件:

CMake cannot deduce the set of header files to be installed from the target. This makes sense, as the target may contain both private and public header files, but CMake does not differentiate between those. As a consequence, you have to list the header files explicitly in an INSTALL(FILES ...) command:

install(FILES ${MY_HEADER_FILES} DESTINATION include)

PUBLIC_HEADER 您偶然发现的字段与OSX框架机制有关,该机制本身就是蠕虫病毒。我建议您不要这样做,除非您实际上想在OSX上将库作为 .framework 部署。

The PUBLIC_HEADER field that you stumbled upon is related to the OSX framework mechanism, which is its very own can of worms. I suggest you stay clear of it, unless you actually want to deploy your library as a .framework on OSX.

请特别注意 包含目的地 INSTALL(TARGET ...)命令的code> 选项。尽管这实际上并不复制任何文件,但它允许将include目录自动添加到config软件包脚本提供的导入目标中。如果您打算向用户提供程序包配置脚本(至少应该在希望用户也使用CMake的情况下,您可能应该这样做),则可能需要设置此选项。

Take special note of the INCLUDES DESTINATION option to the INSTALL(TARGET ...) command. While this does not actually copy any files itself, it allows the include directory to be added to the imported target provided by the config package script automatically. If you intend to provide a package config script to your users (which you probably should, at least if you expect your users to also use CMake), you probably want to set this option.

由于安装机制总体而言非常复杂,因此我有一个小github我的项目,您可以在其中观察所有活动要素。

Since the install mechanism is quite complicated overall, I have a small github project of mine where you can observe all the elements in action.