less than 1 minute read

Categories:

Tags: ,

Normally setting output directory could be done by following commends

set(DEFAULT_CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)

Visual Studio 2022

Visual studo configures the project for several configuration at once. Using VS generator CMAKE_BUILD_TYPE doesnโ€™t contain the configuration name.

By default, variables like CMAKE_LIBRARY_OUTPUT_DIRECTORY are automatically cmake-page

Solution

  1. Use cmake-generator-expressions
    # For Debug configuration this will be evaluated to
     #   '${CMAKE_BINARY_DIR}/${OUTPUT_DEBUG}'
     # For Release configuration this will be evaluated to
     #   '${CMAKE_BINARY_DIR}/${OUTPUT_REL}'
     set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<$<CONFIG:DEBUG>:${OUTPUT_DEBUG}>$<$<CONFIG:RELEASE>:${OUTPUT_REL}>")
    
  2. Append _ behind variable that we are setting. For instance.
    # Output directory for libraries in Debug configuration
     set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/${OUTPUT_DEBUG})
     # Output directory for libraries in Release configuration
     set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/${OUTPUT_REL})
    

reference: stackoverflow

Leave a comment