Wattskemov
Wattskemov

Reputation: 756

How to make CMake to set Visual Studio linker's option Generate Debug Info as yes?

I am using CMake to generate Visual Studio project. In my release edition, I also want to set Visual Studio project's one property as Yes, which is properties ==> Configuration Properties ==> Linker ==> Debugging ==> Generate Debug Info.

Is it possible?

Upvotes: 10

Views: 9547

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54589

You can add custom linker options using the LINK_FLAGS target property:

add_executable(foo ${FOO_SOURCES})
if(MSVC)
    set_property(TARGET foo APPEND PROPERTY LINK_FLAGS /DEBUG)
endif()

If you are already on CMake version 3.13 or higher, you should use the LINK_OPTIONS property instead (thanks to @bsa for pointing this out in the comments). CMake even provides a handy command for setting this:

add_executable(foo ${FOO_SOURCES})
if(MSVC)
    target_link_options(foo PUBLIC /DEBUG)
endif()

This sets the /DEBUG flag for all configurations on Visual Studio builds. It is also possible to add the flag only for a specific configuration.

Note that this really only sets the linker flag and nothing else. If you want a fully functional debug build, you will have to set other flags as well. As pointed out in the comments, you should avoid fiddling with those flags manually and prefer using one of the provided configurations instead, as it can be quite difficult to get right.

Upvotes: 11

Related Questions