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?
In Solution Explorer, right-click the project and choose Properties. In the side pane, choose Build (or Compile in Visual Basic). In the Configuration list at the top, choose Debug or Release. Select the Advanced button (or the Advanced Compile Options button in Visual Basic).
You can also start a debug session from Solution Explorer. First, switch to CMake Targets View in the Solution Explorer window. Then, right-click on an executable and select Debug. This command automatically starts debugging the selected target based on your active configuration.
RelWithDebugInfo means build with debug symbols, with or without optimizations. Anyway, the debug symbols can be stripped later, both using strip and/or obj-copy. If you use strip, the output can be saved.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With