# Link to debug or release binaries

**URL:** <https://discourse.cmake.org/t/link-to-debug-or-release-binaries/4280>\
**Category:** Code\
**Tags:** os:windows, comp:msvc\
**Created:** [October 14, 2021, 11:56am UTC](https://discourse.cmake.org/t/link-to-debug-or-release-binaries/4280 "2021-10-14T11:56:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xhustango](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhustango/32/1614_2.png) [@xhustango](https://discourse.cmake.org/u/xhustango)\
**Post date:** [October 14, 2021, 11:56am UTC](https://discourse.cmake.org/t/link-to-debug-or-release-binaries/4280/1 "2021-10-14T11:56:26Z")

</div>

Hi there!

I am building an app using CMake and wxwidgets. The issue I am facing is that I can’t compile my app in release mode because is using the debug libs. I don’t know how to set the directories to look for in different places depending on the compiling mode.

My current CMake is:

```auto
cmake_minimum_required(VERSION 3.18)

message(STATUS "Building SUN")

set(wxWidgets_USE_DEBUG ON)
set(wxWidgets_USE_UNICODE ON)

set(wxWidgets_ROOT_DIR C:/wx314_install/Debug)
set(wxWidgets_LIB_DIR C:/wx314_install/Debug/lib/vc_x64_lib)
set(wxWidgets_INCLUDE_DIRS C:/wx314_install/Debug/include)

message(STATUS "${wxWidgets_ROOT_DIR}")
# Note that for MinGW users the order of libs is important!

find_package(wxWidgets REQUIRED net gl core base)

include(${wxWidgets_USE_FILE})

add_executable(app WIN32)

target_sources(app PRIVATE
        src/app.cpp)

target_link_libraries(app PRIVATE ${wxWidgets_LIBRARIES})

```

As you can see, I have the wxwidgets directories links to the Debug folder, I have another folder called Release where I have the release binaries of CMake.

How can I set the different wxwidgets variables to use one or other binaries to compile my project?

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![xhustango](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhustango/32/1614_2.png) [@xhustango](https://discourse.cmake.org/u/xhustango)\
**Post date:** [October 14, 2021, 12:21pm UTC](https://discourse.cmake.org/t/link-to-debug-or-release-binaries/4280/2 "2021-10-14T12:21:20Z")

</div>

At the moment I am doing the following as a workaround:

```auto
IF(CMAKE_BUILD_TYPE MATCHES Release)
    message(STATUS "Release mode" )
    set(wxWidgets_ROOT_DIR C:/wx314_install/Release)
    set(wxWidgets_LIB_DIR C:/wx314_install/Release/lib/vc_x64_lib)
    set(wxWidgets_INCLUDE_DIRS C:/wx314_install/Release/include)
ENDIF()

IF(CMAKE_BUILD_TYPE MATCHES Debug)
    message(STATUS "Debug mode" )
    set(wxWidgets_ROOT_DIR C:/wx314_install/Debug)
    set(wxWidgets_LIB_DIR C:/wx314_install/Debug/lib/vc_x64_lib)
    set(wxWidgets_INCLUDE_DIRS C:/wx314_install/Debug/include)
ENDIF()

```

but I don’t know if there is cleaner way.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [October 15, 2021, 11:34pm UTC](https://discourse.cmake.org/t/link-to-debug-or-release-binaries/4280/3 "2021-10-15T23:34:09Z")

</div>

The cleaner way would be to have imported targets with config-specific paths:

```cmake
add_library(wxWidgets::foolib UNKNOWN IMPORTED)
set_target_properties(wxWidgets::foolib PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "$<IF:$<CONFIG:Debug>,${debug_root}/include,${release_root}/include>"
  IMPORTED_LOCATION_RELEASE "${release_root}/lib/…"
  IMPORTED_LOCATION_DEBUG "${debg_root}/lib/…")

```

You may need to [map configurations](https://cmake.org/cmake/help/latest/prop_tgt/MAP_IMPORTED_CONFIG_CONFIG.html) for `RelWithDebInfo` or `MinSizeRel`.

---

<div class="post-metadata">

**Author:** ![xhustango](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhustango/32/1614_2.png) [@xhustango](https://discourse.cmake.org/u/xhustango)\
**Post date:** [October 18, 2021, 8:01am UTC](https://discourse.cmake.org/t/link-to-debug-or-release-binaries/4280/4 "2021-10-18T08:01:17Z")

</div>

Hi Ben,

I am doing the exact same thing with OpenCV. That was one hell of a job because I am using many libraries for different executables and libraries and trying to identify all of them was a bit tuff.  
Here is the example:

```auto
add_library(opencv_core STATIC IMPORTED)
set_property(TARGET opencv_core APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(opencv_core PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
        IMPORTED_LOCATION_DEBUG "${PROJECT_SOURCE_DIR}/3rdparty/ImageAccumulation/3rdparty/OpenCV/lib/Debug/opencv_core411d.lib")

set_property(TARGET opencv_core APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(opencv_core PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
        IMPORTED_LOCATION_RELEASE "${PROJECT_SOURCE_DIR}/3rdparty/ImageAccumulation/3rdparty/OpenCV/lib/Release/opencv_core411.lib")
set_target_properties(opencv_core PROPERTIES
        MAP_IMPORTED_CONFIG_MINSIZEREL Release
        MAP_IMPORTED_CONFIG_RELWITHDEBINFO Release)

```

That is done for each .lib file. The example that you wrote it’s for a whole directory instead? I would like to avoid doing 100 lines in CMake to populate each .lib.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [October 18, 2021, 11:16am UTC](https://discourse.cmake.org/t/link-to-debug-or-release-binaries/4280/5 "2021-10-18T11:16:08Z")

</div>

No, my example is per-library. However, OpenCV uses CMake and should be installing its own `opencv-config.cmake` file that you can use directly with `find_package(OpenCV)`.

---

<div class="post-metadata">

**Author:** ![xhustango](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhustango/32/1614_2.png) [@xhustango](https://discourse.cmake.org/u/xhustango)\
**Post date:** [October 18, 2021, 12:22pm UTC](https://discourse.cmake.org/t/link-to-debug-or-release-binaries/4280/6 "2021-10-18T12:22:01Z")

</div>

Yes, same as wxWidgets, but currently the setup that was made by a another guy that is not here, it was badly done. So I am refactoring the whole project setup at the moment.

Thank you for your comments, Ben.
