C++ static variable in .lib does not initialize

I had a similar problem and solved it by setting the lib project as a dependency of the main app project and then setting ‘Link Library Dependencies’ and ‘Use Library Dependency Inputs’ to Yes for the main project.


Update:

Recently I discovered that Visual Studio 2015 now supports a /WHOLEARCHIVE linker flag. I can’t find it through the linker options, but you can add it as an additional command line option. It works similar to the GCC flag -whole-archive and you add it to your target linker flags (not to the static lib flags).

For example, specify /WHOLEARCHIVE:lib_name as an additional linker command line option and it will include all symbols from that lib. You can do this more than one lib as well.

If you use this /WHOLEARCHIVE:lib_name you no longer need the ‘Link Library Dependencies’ and ‘Use Library Dependency Inputs’ set to Yes. This is perfect for solutions generated through CMAKE. See a related answer here: https://stackoverflow.com/a/42083877/1151329

Leave a Comment