views:

43

answers:

3

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 object 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.

A: 

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

Jerry Coffin
A: 

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

plodoc
+1  A: 

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.

paul_71
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