CMAKE for /DEF and /NODEFAULTLIB
Asked Answered
C

2

10

How do I add linker-flags "DEF" and "NODEFAULTLIB" to vs2012 project via CMAKE?

Cynosure answered 13/9, 2013 at 12:46 Comment(0)
J
12

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()
Joinder answered 13/9, 2013 at 18:45 Comment(6)
SET_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"..Cynosure
You should be able to do SET_TARGET_PROPERTIES(name PROPERTIES LINK_FLAGS "/DEF:\"ExportedFunctions.def\" /NODEFAULTLIB:\"mfc110d\"")Joinder
Or to CMAKE_SHARED_LINKER_FLAGS, or to CMAKE_LINKER_FLAGS ... oh, no, whatever!Coif
what mean: /DEF:my_defs.def ?Sweetscented
make my da...night. Thank you !Randellrandene
@Fraser: Do you really need the quotes?Valaree
F
2

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.

Frawley answered 11/11, 2019 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.