Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cmake

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?

like image 604
Watterry Avatar asked Jul 21 '14 06:07

Watterry


People also ask

How do I set debug configuration in Visual Studio?

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).

How do I debug CMake project in Visual Studio?

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.

What is release with debug info?

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.


1 Answers

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.

like image 93
ComicSansMS Avatar answered Oct 11 '22 19:10

ComicSansMS