WINDOWS_EXPORT_ALL_SYMBOLS: MSVC LNK4197 when exporting classes with static members explicitly
The target property WINDOWS_EXPORT_ALL_SYMBOLS allows for automatic export of symbols from a shared library. It does not export static members (and never has). Since classes derived from QObjects rely on static meta data, it has always been necessary to explicitly export/import such classes even when enabling the automatic export. Up to and including cmake version 3.26, this worked as expected: The majority of symbols was exported automatically, and a few Qt-related classes had to be exported explicitly.
Using
- cmake 3.28.1 (or 3.27.9),
- VisualStudio 2022 17.9.2,
- and Qt 6.5,
exporting classes explicitly while also enabling WINDOWS_EXPORT_ALL_SYMBOLS leads to the linker warning LNK4197 ("export specified multiple times"). Without the explicit export, you get a linker error on the consuming side for the static data (as expected). The immediate cause for the warning are additional entries of the form "??_7Foo@@6B@" in the export.def file generated by cmake, where "Foo" is the exported class.
Please find attached a minimal example with a shared library and a consuming executable. The example works up to cmake 3.26 and fails with cmake 3.28.1. symbol_export.zip