CMake将共享库链接到静态库 [英] CMake link a shared library to static libraries
问题描述
我正在将AutoTools项目移植到CMake。
AutoTools的功能:
- 构建一些静态库
- 构建一些共享库并将静态库链接到共享中
- 构建可执行文件,将其链接到共享库
-
我用CMake做了什么:
- 构建一些静态库 -
add_library(staticfoo< src>)
- 构建一些共享库 -
add_library(sharedfoo SHARED< src>)
并链接它们 -target_link_libraries(sharedfoo staticfoo)
- 构建可执行文件,将其链接到共享库 -
target_link_libraries(exe sharedfoo)
,但是再次拖动了静态库。
因此,生成的可执行文件的链接命令除了共享之外还有静态库。
我试过了 target_link_libraries(sharedfoo PRIVATE staticfoo)
如何获取没有传递行为的符号? / p>
(以平台无关的方式)
CMake不允许混合STATIC和SHARED库。
如果您的 staticfoo
库仅用作其他库/可执行文件,可以将其定义为
add_library(staticfoo OBJECT< src>)
,然后在构建其他库时用作某种来源:
add_library(sharedfoo SHARED< src> $< TARGET_OBJECTS:staticfoo>)
$ b b
有关详情,请参阅 add_library上的文档 。
I am porting an AutoTools project to CMake.
What AutoTools does:
- builds some static libraries
- builds some shared libraries and links static ones into shared
- builds an executable, links it to shared libraries
What I've managed to do with CMake:
- build some static libraries -
add_library(staticfoo <src>)
- build some shared libraries -
add_library(sharedfoo SHARED <src>)
and link them -target_link_libraries(sharedfoo staticfoo)
- build an executable, link it to shared libraries -
target_link_libraries(exe sharedfoo)
, but that dragged the static libraries in again, too.
So, the resulting link command for the executable has static libs in addition to shared. Which doesn't correspond to the command generated by AutoTools project.
I've tried target_link_libraries(sharedfoo PRIVATE staticfoo)
, but that doesn't get the symbols from the static lib into the interface of the shared lib.
How to get the symbols without that 'transitive' behavior?
(in platform-independent way)
As I know, CMake doesn't allow to mix STATIC and SHARED libraries.
If your staticfoo
library is used solely as part of other libraries/executables, you can define it as
add_library(staticfoo OBJECT <src>)
and use then as some sort of sources when build other library:
add_library(sharedfoo SHARED <src> $<TARGET_OBJECTS:staticfoo>)
For more info see documentation on add_library.
这篇关于CMake将共享库链接到静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!