28

When linking a static library against an executable, unreferenced symbols are normally discarded. In my case some otherwise unused objects are used to register their respective classes into a factory and if the objects are discarded, this registration fails.

Under Unix where we use gcc, I can pass the flag --whole-archive to the linker ld (see excerpt from ld documentation below), which makes ld not discard any objects. Is there anything like this for Visual C++?

--whole-archive

    For each archive mentioned on the command line after the
    `--whole-archive' option, include every object file in the archive
    in the link, rather than searching the archive for the required
    object files. This is normally used to turn an archive file into
    a shared library, forcing every object to be included in the
    resulting shared library. This option may be used more than once.

1
  • Thanks for the explanation of --whole-archive, I was just searching what is the scenario to use it.
    – Deqing
    Mar 25, 2013 at 3:04

7 Answers 7

30

The version of Visual C++ in Visual Studio 2015 Update 2 includes a new flag to link.exe called /WHOLEARCHIVE, which has equivalent functionality to the --whole-archive option to ld. According to the flag documentation:

The /WHOLEARCHIVE option forces the linker to include every object file from either a specified static library, or if no library is specified, from all static libraries specified to the LINK command.

1
  • 1
    Wow, this is huge. Thanks for the info.
    – fschmitt
    Sep 27, 2016 at 12:51
9

To my knowledge, there is no single option which reliably guarantees that. There are combinations of optimizing options which (silently) deactivate this, so no way... /INCLUDE works, but for that you need to extract and hardcode the mangled name of the symbol. You have two choices: (1) ensure, that all registrars are contained (included) in the translation unit containing main and enforce their usage. (2) Give up this 'idiom' and use explicit registration.

Caution: this answer is now almost 7 years old and the statements regarding the availability of options in the MSVC++ toolchain are outdated. Nevertheless I still recommend not to rely on registrar pattern and look at the alternatives.

4
  • Thanks. I went the route to create a small program which extracts all symbol names from the static lib and includes them in the main program.
    – fschmitt
    Oct 6, 2010 at 6:42
  • 1
    Apparently this got silently downvoted two times after almost two years. Could you please drop a line about the reason? Feb 10, 2012 at 14:31
  • @fschmitt did you ever find a better solution to this problem?
    – rr-
    Dec 20, 2015 at 0:13
  • 1
    I went from the static library to a dll which solved this, as no symbols are discarded from dynamic libraries.
    – fschmitt
    Jan 4, 2016 at 9:13
6

I believe about the closest equivalent would be /OPT:NOREF.

2
  • 2
    Doesn't seem to make any effect, esp. considering it's implied in debug builds.
    – rr-
    Dec 20, 2015 at 0:12
  • That should, but doesn't work at all, and not just for me. Very unfortunate. Sep 4, 2016 at 19:45
6

You can use with CMake like:

add_executable(hello ${SOURCE_FILES})
target_link_libraries(hello libA libB libC)     # Not need /wholearchive libC 
set_target_properties(hello PROPERTIES LINK_FLAGS "/WHOLEARCHIVE:libA /WHOLEARCHIVE:libB")

Note: /WHOLEARCHIVE is only available Visual Studio 2015 Update 2+

4

I use /INCLUDE: to force inclusion of unused symbols.

1

I used another approach - rather than compiling everything to a .lib and then link that .lib to the executable, I link the executable directly against the .obj files.

In CMake, this can be made like this:

add_library(common OBJECT ${common_sources})
add_executable(executable1 "main1.cc" $<TARGET_OBJECTS:common>
add_executable(executable2 "main2.cc" $<TARGET_OBJECTS:common>

Changing any of the files in ${common_sources}) only recompiles their equivalent objects and relinks the executables, which provides the same benefits as if you linked things through intermediate .lib. At the same time, all the static constructors remain in place, which resolves the issue.

Note that this is only useful if you link things statically.

This approach was tested with gcc 5.2.0, MinGW-w64 5.2.0 and MSVC 15.

0

In the property page of the executable look at Common Properties/References/Use Library Dependency Inputs set that to true. That's pretty much the MS equivalent of --whole-archive in a nutshell.

Edit: However the library in question needs to be part of the solution.

1
  • No, it adds all the obj files to the linker command instead of the lib file(s). Jun 8, 2016 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.