Search code examples
visual-studiocmakeinstallationtargets

Why cmake install() looks for output files in Release folder for VS


this is a simplified version of my cmake

cmake_minimum_required(VERSION 2.8.4)
project(math)

add_library(math math.cpp)

function(install_package)
    install(TARGETS math 
        LIBRARY DESTINATION lib 
        ARCHIVE DESTINATION lib)
    add_custom_command(TARGET math 
        POST_BUILD 
        COMMAND cmake ARGS -P cmake_install.cmake)
endfunction()

install_package()

But when I build Debug version I get the following error

CMake Error at cmake_install.cmake:55 (file):
  file INSTALL cannot find
  "<my project's root>/build/Release/math.lib".

Why is it looking in Release folder despite that I build for Debug? When I build for Release then, obviously, everything works. I tried to add CONFIGURATIONS option to install method, but it doesn't help. I'm using Visual Studio 15.


Solution

  • If I look into my cmake_install.cmake, Release is the default if you don't specify anything in your add_custom_command() call:

    # Set the install configuration name.
    if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)
      if(BUILD_TYPE)
        string(REGEX REPLACE "^[^A-Za-z0-9_]+" ""
               CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}")
      else()
        set(CMAKE_INSTALL_CONFIG_NAME "Release")
      endif()
      message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"")
    endif()
    

    So if you look into INSTALL.vcxproj the call that CMake is generating looks like:

    "C:\Program Files\CMake\bin\cmake.exe" -DBUILD_TYPE=$(Configuration) -P cmake_install.cmake
    

    Which would translate into:

    add_custom_command(TARGET math 
        POST_BUILD 
        COMMAND ${CMAKE_COMMAND} ARGS -D BUILD_TYPE=$<CONFIG> -P cmake_install.cmake)