为什么 CMake EXPORT 需要用于构建共享库的静态库? [英] Why does CMake EXPORT require static libraries used to build a shared library?

查看:78
本文介绍了为什么 CMake EXPORT 需要用于构建共享库的静态库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让其他项目更容易链接到我们随项目分发的共享库.当我尝试利用 EXPORT 机制时,CMake 抱怨我没有在导出集中包含用于构建共享库的静态库.这对我来说似乎没有必要,因为其他项目只需要链接共享库,而我真的不想安装静态库.这似乎与这个错误非常相似,但我可能只是误解这一切是如何运作的.这是一个最小的例子:

I'm attempting to make it easier for other projects to link against the shared libraries we distribute with our project. When I try to take advantage of the EXPORT mechanism, CMake complains that I'm not including the static libraries used to build the shared libraries in the export set. This seems unnecessary to me as the other projects only need to link against the shared library and I don't really want to install the static libraries. This seems to be very similar to this bug, but I might just be misunderstanding how this all works. Here is a minimal example:

CMAKE_MINIMUM_REQUIRED(VERSION 3.2.1) 
PROJECT(ExportTest)

ADD_LIBRARY(myStaticLib STATIC staticLib.c)
ADD_LIBRARY(mySharedLib SHARED sharedLib.c)
TARGET_LINK_LIBRARIES(mySharedLib myStaticLib)

INSTALL(TARGETS mySharedLib EXPORT myExport DESTINATION lib)
INSTALL(EXPORT myExport DESTINATION include)

导致以下错误消息:

CMake Error: install(EXPORT "myExport" ...) includes target "mySharedLib"
which requires target "myStaticLib" that is not in the export set.

推荐答案

当使用 target_link_libraries 像您一样,库依赖项默认是可传递的.试试:

When using target_link_libraries like you did, library dependencies are transitive by default. Try:

target_link_libraries(mySharedLib PRIVATE myStaticLib)

这篇关于为什么 CMake EXPORT 需要用于构建共享库的静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆