# How to specify different variable value for different build type for visual studio ?

**URL:** https://discourse.cmake.org/t/how-to-specify-different-variable-value-for-different-build-type-for-visual-studio/1216
**Category:** Usage
**Tags:** os:windows, gen:vs
**Created:** [May 14, 2020, 1:28pm UTC](https://discourse.cmake.org/t/how-to-specify-different-variable-value-for-different-build-type-for-visual-studio/1216 "2020-05-14T13:28:14Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![davied9](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davied9/32/601_2.png) [@davied9](https://discourse.cmake.org/u/davied9)
#### Post date: [May 14, 2020, 1:28pm UTC](https://discourse.cmake.org/t/how-to-specify-different-variable-value-for-different-build-type-for-visual-studio/1216/1 "2020-05-14T13:28:14Z")

</div>

I’m trying to generate a simple c++ project with cmake for visual studio.

CMakeLists.txt

```auto
cmake_minimum_required(VERSION 3.5.1)
project(simple)

get_filename_component(LIB_PATH_RELEASE ../release/lib ABSOLUTE)
get_filename_component(LIB_PATH_DEBUG ../debug/lib ABSOLUTE)

add_executable(simple "main.cc")

target_include_directories(simple PUBLIC ../include)

target_link_libraries(simple ${LIB_PATH}/a_lib)

```

Here is what I’m expecting after running “cmake …”

simple.sln  
++ simple.vcxproj for Debug configuration  
++++ use `../debug/lib/a_lib.lib` for linking  
++ simple.vcxproj for Release configuration  
++++ use `../release/lib/a_lib.lib` for linking

I’m aware that my CMakeLists.txt can’t do what I’m expecting, how can I fix my CMakeLists.txt ?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [May 14, 2020, 4:02pm UTC](https://discourse.cmake.org/t/how-to-specify-different-variable-value-for-different-build-type-for-visual-studio/1216/2 "2020-05-14T16:02:36Z")

</div>

Where come from your library `a_lib`?

- If it is built by `CMake`, you can use the target as is in your `target_link_libraries()` command. In this case the `debug` and `release` versions of the library is handled by `CMake`:

```cmake
add_library (a_lib SHARED ...)
...
target_link_libraries (simple PRIVATE a_lib)

```

- If it is an external library, you can achieve what you want by using [generator expressions](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html):

```auto
target_link_libraries (simple PRIVATE "$<IF:$<CONFIG:Debug>,${LIB_PATH_DEBUG}/a_lib.lib,${LIB_PATH_RELEASE}/a_lib.lib>")

```

---

<div class="post-metadata">

### Author: ![davied9](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davied9/32/601_2.png) [@davied9](https://discourse.cmake.org/u/davied9)
#### Post date: [May 15, 2020, 1:43am UTC](https://discourse.cmake.org/t/how-to-specify-different-variable-value-for-different-build-type-for-visual-studio/1216/3 "2020-05-15T01:43:58Z")

</div>

In my post, the a\_lib is a pre-built library, I only need to link to it.  
But in my practical situation, it is another library generated by cmake, and I need to link to its binary.

Either way, your answer helped me out, thanks !

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [May 15, 2020, 12:52pm UTC](https://discourse.cmake.org/t/how-to-specify-different-variable-value-for-different-build-type-for-visual-studio/1216/4 "2020-05-15T12:52:40Z")

</div>

If your library is generated through another build using `CMake`, the best approach, if possible, is to use the mechanism of [EXPORT/IMPORT](https://cmake.org/cmake/help/git-master/command/install.html#export).

---

<div class="post-metadata">

### Author: ![davied9](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davied9/32/601_2.png) [@davied9](https://discourse.cmake.org/u/davied9)
#### Post date: [May 16, 2020, 7:41am UTC](https://discourse.cmake.org/t/how-to-specify-different-variable-value-for-different-build-type-for-visual-studio/1216/5 "2020-05-16T07:41:26Z")

</div>

Thanks you, Marc Chevrier @marc.chevrier.

So I’ll make a summery here:

### 1. cmake target\_link\_libraries

```auto
    add_library (a_lib SHARED ...)
    target_link_library(simple PRIVATE a_lib)

```

or

```auto
    target_link_library(simple PRIVATE debug ${LIB_PATH_DEBUG}/a_lib)
    target_link_library(simple PRIVATE optimized ${LIB_PATH_RELEASE}/a_lib)

```

### 2. [generator expressions](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html)

```auto
target_link_libraries (simple PRIVATE "$<IF:$<CONFIG:Debug>,${LIB_PATH_DEBUG}/a_lib.lib,${LIB_PATH_RELEASE}/a_lib.lib>")

```

### 3. [install EXPORT](https://cmake.org/cmake/help/git-master/command/install.html#export)

We have 2 options to fix how to export the library.

##### 3.1 Change how cmake export the library.

Fix the CMakeLists.txt, change the value for installation directory.

Here, I’m building gRPC with cmake, so I’ve done this

```auto
set(gRPC_INSTALL_LIBDIR lib/$<IF:$<CONFIG:Debug>,debug,release>
    CACHE PATH "Installation directory for libraries of gRPC" FORCE)

```

##### 3.2 Fix exported library targets settings

Which means fixing gRPCTargets-\*.cmake (generated by cmake)

gRPCTargets-debug.cmake

```auto
# Import target "gRPC::zlibstatic" for configuration "Debug"
set_property(TARGET gRPC::zlibstatic APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(gRPC::zlibstatic PROPERTIES
  IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  IMPORTED_LOCATION_DEBUG "${_IMPORT_PREFIX}/lib/ **debug** /zlibstaticd.lib"
  )

```

gRPCTargets-release.cmake

```auto
# Import target "gRPC::zlibstatic" for configuration "Release"
set_property(TARGET gRPC::zlibstatic APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(gRPC::zlibstatic PROPERTIES
  IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/ **release** /zlibstatic.lib"
  )

```

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [May 16, 2020, 10:03am UTC](https://discourse.cmake.org/t/how-to-specify-different-variable-value-for-different-build-type-for-visual-studio/1216/6 "2020-05-16T10:03:07Z")

</div>

For case 3, you cannot use generator expressions in output arguments. And using correctly `CMake`, you don’t have to patch anything.

To manage transparently both debug and release builds, you can proceed like this:

1. Install and debug and release configurations:

2. And include the exported config file to consume the exported library:

---

<div class="post-metadata">

### Author: ![davied9](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davied9/32/601_2.png) [@davied9](https://discourse.cmake.org/u/davied9)
#### Post date: [May 18, 2020, 6:57am UTC](https://discourse.cmake.org/t/how-to-specify-different-variable-value-for-different-build-type-for-visual-studio/1216/7 "2020-05-18T06:57:06Z")

</div>

In 1st fix, I’ve noticed that you missed ‘ **EXPORT my\_lib** ’ on purpose, then for **Release** configuration, because a\_lib is already exported, then CMake will export the **Release** configuration of ‘a\_lib’ by itself, right ?
