CMake link a shared library to static libraries

2019-06-26 16:51发布

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)

标签: c++ c cmake
4条回答
一夜七次
2楼-- · 2019-06-26 17:34

See my answer here. Basically add /WHOLEARCHIVE, -all_load, or --whole-archive to the linker flags.

查看更多
劳资没心,怎么记你
3楼-- · 2019-06-26 17:35

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.

查看更多
等我变得足够好
4楼-- · 2019-06-26 17:37

I created a little example that does this the right way, here: https://github.com/CarloWood/cmaketest

It shows how two "static" libraries, one with two visible symbols, both with by default hidden symbols, are added to a shared library, which is then used to link an executable.

The method used is that of @Tsyvarev, combined with the VISIBILITY target property mentioned by @Zaufi.

The only non-platform specific thing (I think) is the __EXPORT macro that is used. It would be nice to fix that too, if anyone knows how.

查看更多
Summer. ? 凉城
5楼-- · 2019-06-26 17:42

To resolve this case you need to do few things:

  • first of all make sure you've compiled static libraries w/ -fPIC, so they'll contain a relocatable code (which would be a part of a shared library later)
  • then, you need to control symbols visibility when compiling all libraries, so being a part of shared library, symbols came from the static one would be visible
  • and finally, yes, you need to specify PRIVATE <static libs> when linking your shared library, so the linker command line for your executable wouldn't have any static libs
查看更多
登录 后发表回答