CMake shared libraries on Visual Studio / oneAPI

CMake requires target parameter WINDOWS_EXPORT_ALL_SYMBOLS set to true to properly use shared libraries on Windows with Visual Studio or Intel oneAPI (which uses Visual Studio as a backend on Windows). For convenience CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS sets all targets default parameter. Without this parameter, linking fails because the “.lib” import library isn’t created with the exported shared library symbols.

An example CMakeLists.txt where this parameter is needed to build successfully with Visual Studio or Intel oneAPI on Windows:

set(BUILD_SHARED_LIBS true)

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS true)

add_library(a a.c)
add_library(b b.c)

target_link_libraries(b PRIVATE a)

It’s also possible to set this parameter for individual targets like:

set_target_properties(a PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS true)