How do I add linker-flags "DEF" and "NODEFAULTLIB" to vs2012 project via CMAKE?
2 Answers
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()

- 74,704
- 20
- 238
- 215
-
2SET_TARGET_PROPERTIES(name PROPERTIES LINK_FLAGS -DEF:"ExportedFunctions.def" -NODEFAULTLIB:"mfc110d" ) this does not work, i have "module definition file", but no "ignore specific default library".. – etwas77 Sep 16 '13 at 06:51
-
2You should be able to do `SET_TARGET_PROPERTIES(name PROPERTIES LINK_FLAGS "/DEF:\"ExportedFunctions.def\" /NODEFAULTLIB:\"mfc110d\"")` – Fraser Sep 16 '13 at 18:42
-
Or to CMAKE_SHARED_LINKER_FLAGS, or to CMAKE_LINKER_FLAGS ... oh, no, whatever! – Liviu Oct 01 '15 at 06:47
-
what mean: /DEF:my_defs.def ? – ilw Nov 05 '17 at 18:06
-
make my da...night. Thank you ! – user1503944 Nov 24 '18 at 22:21
-
@Fraser: Do you really need the quotes? – einpoklum Apr 22 '23 at 08:49
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.

- 16,549
- 8
- 60
- 74

- 2,358
- 2
- 25
- 28