Skip to content

Correct place to add /WHOLEARCHIVE

I have a static library that I am linking into my target which needs to have all symbols included. This is done using the /WHOLEARCHIVE:my_static_lib flag for MSVC and wrapping it in -Wl,--whole-archive my_static_lib -Wl,--no-whole-archive for GNU.

How does one go about achieving this in CMake? We have something working for the GNU version, but I cannot figure out the MSVC version. The GNU solution is also quite unobvious.

Our current solution looks as follows:

add_executable(my_exe main.cpp)
set_target_properties(my_exe PROPERTIES STATIC_LIBRARY_OPTIONS $<$<CXX_COMPILER_ID:MSVC>:/WHOLEARCHIVE:my_static_lib>)
# This needs to be a separate target_link_libraries call, otherwise the flags get shuffled with the rest of the libraries
target_link_libraries(my_exe 
    PRIVATE
        $<$<CXX_COMPILER_ID:GNU>:-Wl,--whole-archive>
        $<$<CXX_COMPILER_ID:GNU>:my_static_lib>
        $<$<CXX_COMPILER_ID:GNU>:-Wl,---no-whole-archive>
        # $<$<CXX_COMPILER_ID:MSVC>:/WHOLEARCHIVE:my_static_lib> this does not work
)
target_link_libraries(my_exe 
    PRIVATE
        ... other libraries
)
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information