Search code examples
c++visual-c++visual-studio-2013cmakecmake-gui

Have custom Find*.cmake file find different libraries depending on visual studio build type (Debug/Release)


I am creating a few C++ libraries in a project (solution in VS terminology) that needs to be used by two other projects. For this I created a FindDQSAnalyticsInfra.cmake file which is as follows:

# DQSAnalyticsInfra
# -----
# Find the path to DQSAnalyticsInfra header files and libraries
#
# DEFINES
# ------
# DQSINFRA_ROOT - Root of the DQSAnalyticsInfra project
# DQSINFRA_INCLUDE_DIR - DQSAnalyticsInfra include directory
# DQSINFRA_LIBRARIES - Libraries required to link DQSAnalyticsInfra
# DQSINFRA_FOUND - Confirmation

set(DQSINFRA_LIBRARIES_LIST Utils Actor gtest)

find_path(DQSINFRA_INCLUDE_DIR Actor/Actor.h Utils/Log.h gtest/gtest/gtest.h
      ${DQSINFRA_ROOT}/include
      )

foreach(search_lib ${DQSINFRA_LIBRARIES_LIST}) 
    find_library(DQSINFRA_LIB NAMES ${search_lib}
                 PATHS
                 ${DQSINFRA_ROOT}/lib/Release #The problem is here
                )
    set(DQSINFRA_LIBRARIES ${DQSINFRA_LIBRARIES} ${DQSINFRA_LIB})
    if(DQSINFRA_LIB)
        unset(DQSINFRA_LIB CACHE)
        set(DQSINFRA_FOUND TRUE)
    else(DQSINFRA_LIB)
        set(DQSINFRA_FOUND FALSE)
        break()
    endif(DQSINFRA_LIB)
endforeach(search_lib)

if(DQSINFRA_INCLUDE_DIR AND DQSINFRA_LIBRARIES AND DQSINFRA_FOUND)
    set(DQSINFRA_FOUND TRUE)
    message(STATUS "Found DQSAnalyticsInfra. ")
    message(STATUS "Include Path: ${DQSINFRA_INCLUDE_DIR}")
    message(STATUS "Libraries ${DQSINFRA_LIBRARIES}")
else(DQSINFRA_INCLUDE_DIR AND DQSINFRA_LIBRARIES AND DQSINFRA_FOUND)
    set(DQSINFRA_FOUND FALSE)
    message(STATUS "DQSAnalyticsInfra not found.")
endif(DQSINFRA_INCLUDE_DIR AND DQSINFRA_LIBRARIES AND DQSINFRA_FOUND)

mark_as_advanced(DQSINFRA_INCLUDE_DIR DQSINFRA_LIBRARIES)

This file works fine. The issue is that in the find_library command used in this file, I am hardcoding the path as ${DQSINFRA_ROOT}/lib/Release. This mean that I cannot use this file to link to Debug builds (I have to manually change the file to use ${DQSINFRA_ROOT}/lib/Debug). Any idea on how this can be fixed.Thanks.


Solution

  • Use debug and optimized keywords that can be specified for target_link_libraries:

    find_library(DQSINFRA_LIB_DEBUG NAMES ${search_lib} 
                     PATHS
                     ${DQSINFRA_ROOT}/lib/Debug
                    )
    
    find_library(DQSINFRA_LIB_RELEASE NAMES ${search_lib}
                     PATHS
                     ${DQSINFRA_ROOT}/lib/Release
                    )
    
    set(DQSINFRA_LIBRARIES optimized ${DQSINFRA_LIB_RELEASE} debug ${DQSINFRA_LIB_DEBUG})