I have a library which symbols should be imported to an app despite the fact they aren't used explicitly there. The purpose of the library is to execute static constructors and thus make itself available to an app at runtime (register its symbols).
I've found the corresponding flags (gcc): -Wl,--no-as-needed
for shared library and --whole-archive
for static. In cmake I can set a variable LINK_WHAT_YOU_USE
to control --no-as-needed
flag and STATIC_LIBRARY_OPTIONS
but they will be applied to all libraries listed next after. What I need is to apply them only for one specific library, ie -Wl,--no-as-needed -lmylib -Wl,--as-needed
and -Wl,--whole-archive -lmylib -Wl,--no-whole-archive
.
How to do this with the help of cmake in a cross-platform way?
UPD. I've found a way to do this but it doesn't look "cross-platform" enough:
target_link_libraries(
myapp PRIVATE
"-Wl,--no-as-needed -Wl,--whole-archive" mylib "-Wl,--as-needed -Wl,--no-whole-archive")
It works on linux(ld) but I doubt it will be on windows/mac. Is there a way to do the same without hard-coded strings using cmake functions/variables?
Is there a way to do the same without hard-coded strings using cmake functions/variables?
As of now, no there is not. You have to write -Wl,--whole-archive
yourself.
I believe it would be nice to have some set_target_properties(static_target PROPERTIES WHOLE_ARCHIVE true)
in cmake, but I also guess that implementation might be hard.
If you want to be cross-platform, you would detect it:
check_c_compiler_flag('-Wl,--whole-archive' has_whole_archive)
if (has_whole_archive)
use_it(-Wl,--whole-archive)
endif()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.