CMAKE for /DEF and /NODEFAULTLIB
You can append them to CMAKE_EXE_LINKER_FLAGS
:
if(MSVC)
set(CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} /DEF:my_defs.def /NODEFAULTLIB")
endif()
The general way is to add linker flags to CMAKE_xxx_LINKER_FLAGS
, yes. However in case of CMAKE_SHARED_LINKER_FLAGS
and /DEF:
parameter, there is a special case that made me run into trouble.
If you already use CMAKE_EXPORT_ALL_SYMBOLS
(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS
) then the /DEF:
parameter will not appear in the linking command, even if you specified it in CMAKE_SHARED_LINKER_FLAGS
.
This is because MSVC linker accepts only one /DEF:
parameter, and CMake does not want to override the existing one: /DEF:<build_path>/exports.def
(which is added due to CMAKE_EXPORT_ALL_SYMBOLS
) with the one which you specify in the CMAKE_SHARED_LINKER_FLAGS
.
You can use also CMAKE_CXX_STANDARD_LIBRARIES
. It adds arbitrary linker flags without checking them, but it adds them into the middle of the linking command before the /DEF:<build_path>/exports.def
so that the latter won't get overridden.
The full discussion of this case here: https://cmake.org/pipermail/cmake-developers/2019-November/031274.html.