Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portable whole-archive linking in CMake

Tags:

linker

cmake

If you want to link a static library into an shared library or executable while keeping all the symbols visible (e.g. so you can dlopen it later to find them), a non-portable way to do this on Linux/BSD is to use the flag -Wl,--whole-archive. On macOS, the equivalent flag is -Wl,-force_load,<library>; on Windows it's apparently /WHOLEARCHIVE.

Is there a portable way to do this in CMake?

I know I can add linker flags with target_link_libraries. I can detect the OS. However, since the macOS version of this includes the library name in the same string as the flag (no spaces), I think this messes with CMake's usual handling of link targets and so on. The more compatible I try to make this, the more I have to bend over backwards to make it happen.

And this is without even getting into more unusual compilers like Intel, PGI, Cray, IBM, etc. Those may not be compilers that people commonly deal with, but in some domains it's basically unavoidable to need to deal with these.

Are there any better options?

like image 393
Elliott Slaughter Avatar asked Aug 17 '18 04:08

Elliott Slaughter


1 Answers

  • flink.cmake will help you.
target_force_link_libraries(<target>
    <PRIVATE|PUBLIC|INTERFACE> <item>...
    [<PRIVATE|PUBLIC|INTERFACE> <item>...]...
)
like image 135
horance liu Avatar answered Oct 08 '22 19:10

horance liu