Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CMake, how do I work around the Debug and Release directories Visual Studio 2010 tries to add?

I'm trying to build one of my CMake-based projects from a couple of years ago with Visual Studio 2010 and I'm running into problems to do with the output directory for a project. Visual Studio has always been very keen on adding Debug/ and Release/ subdirectories when outputting binaries, and for various reasons I've always been very keen on removing them - now that I'm using a new version of CMake and a new version of Visual Studio, the old workaround in CMake no longer seems to work, and I'm looking to find out the "new" way of doing it.

With a previous version of CMake (2.6) and a previous version of Visual Studio (2008), I used the following:

IF(MSVC_IDE)     # A hack to get around the "Debug" and "Release" directories Visual Studio tries to add     SET_TARGET_PROPERTIES(${targetname} PROPERTIES PREFIX "../")     SET_TARGET_PROPERTIES(${targetname} PROPERTIES IMPORT_PREFIX "../") ENDIF(MSVC_IDE) 

This worked fine, but no longer seems to do the trick. Please does anyone know of a similar but more up-to-date workaround that will work with CMake 2.8.6 and Visual Studio 2010?

like image 274
Stuart Golodetz Avatar asked Oct 12 '11 23:10

Stuart Golodetz


People also ask

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.


2 Answers

It depends a bit on what you want precisely, but I would recommend to take a look at the available target properties, similar to this question.

It depends a bit on what you want exactly. For each target, you could manually set the library_output_directory or runtime_output_directory properties.

if ( MSVC )     set_target_properties( ${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${youroutputdirectory} )     set_target_properties( ${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_DEBUG ${youroutputdirectory} )     set_target_properties( ${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_RELEASE ${youroutputdirectory} )     # etc for the other available configuration types (MinSizeRel, RelWithDebInfo) endif ( MSVC ) 

You could also do this globally for all sub-projects, using something like this:

# First for the generic no-config case (e.g. with mingw) set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${youroutputdirectory} ) set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${youroutputdirectory} ) set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${youroutputdirectory} ) # Second, for multi-config builds (e.g. msvc) foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )     string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )     set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory} )     set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory} )     set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory} ) endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES ) 
like image 161
André Avatar answered Oct 06 '22 00:10

André


In current versions of CMake you can use a generator expression for LIBRARY_OUTPUT_DIRECTORY to avoid the configuration-specific suffix.

I just added $<$<CONFIG:Debug>:>, which always expands to nothing, to mine. This looks a bit weird, but it does work, and it's not so weird you can't explain it with a brief comment:

# Use a generator expression so that the specified folder is used directly, without any # configuration-dependent suffix. # # See https://cmake.org/cmake/help/v3.8/prop_tgt/LIBRARY_OUTPUT_DIRECTORY.html set_target_properties(library PROPERTIES                       LIBRARY_OUTPUT_DIRECTORY my/folder/$<$<CONFIG:Debug>:>) 
like image 26
Tom Seddon Avatar answered Oct 05 '22 23:10

Tom Seddon