# Link external static lib along with dynamic libs to the executable

**URL:** https://discourse.cmake.org/t/link-external-static-lib-along-with-dynamic-libs-to-the-executable/6309
**Category:** Code
**Tags:** os:windows
**Created:** [August 20, 2022, 11:52am UTC](https://discourse.cmake.org/t/link-external-static-lib-along-with-dynamic-libs-to-the-executable/6309 "2022-08-20T11:52:28Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)
#### Post date: [August 20, 2022, 11:52am UTC](https://discourse.cmake.org/t/link-external-static-lib-along-with-dynamic-libs-to-the-executable/6309/1 "2022-08-20T11:52:28Z")

</div>

Hi,

Can’t understand why linker gives me error:

```auto
licensecc_static.lib(licensecc.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' d
oesn't match value 'MD_DynamicRelease' in Main.obj

```

I externally compiler `licensecc_static.lib` as static library and I’m trying to link it to executable.  
Also I link few shared libs to the same executable.

I tried to:

```cmake
  set_property(TARGET licensecc::licensecc_static PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

```

but this didn’t help.

All of them compiled in Release mode with MSVC 2022

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [August 20, 2022, 2:17pm UTC](https://discourse.cmake.org/t/link-external-static-lib-along-with-dynamic-libs-to-the-executable/6309/2 "2022-08-20T14:17:21Z")

</div>

The runtime of all objects (including those from libs) must match.  
Currently you’re compiling your library against the static and `Main.obj` against the DLL runtime. Both in release, not debug mode.  
The manual of [MSVC\_RUNTIME\_LIBRARY](https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html) shows all 4 possible values.

---

<div class="post-metadata">

### Author: ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)
#### Post date: [August 20, 2022, 6:49pm UTC](https://discourse.cmake.org/t/link-external-static-lib-along-with-dynamic-libs-to-the-executable/6309/3 "2022-08-20T18:49:49Z")

</div>

Thank you for the explanation.

It seems I don’t understand something.  
For now I just compiled `licensecc` library without [-DSTATIC\_RUNTIME=1](http://open-license-manager.github.io/licensecc/development/Build-the-library-windows.html#supported-cmake-definitions-options) even though it is recommended to set it to `1`. It works if it is `0` (no mentionned errors).

But what `STATIC_RUNTIME` really changes?  
As I see if I pass `STATIC_RUNTIME=1` then `/MT` flag is added ([link for the code](https://github.com/open-license-manager/licensecc/blob/eb882ae75b92827a245542e7ddfc48f9476d67a7/CMakeLists.txt#L88)).  
Why adding `/MT` flag leads to mentionned error: after all the library is added using [ADD\_LIBRARY(licensecc\_static STATIC ${sources})](https://github.com/open-license-manager/licensecc/blob/eb882ae75b92827a245542e7ddfc48f9476d67a7/src/library/CMakeLists.txt#L6-L15) - it is still static.

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [August 21, 2022, 10:21am UTC](https://discourse.cmake.org/t/link-external-static-lib-along-with-dynamic-libs-to-the-executable/6309/4 "2022-08-21T10:21:49Z")

</div>

The type of the MSVC runtime is unrelated to the type of library you want to build. It’s totally possible to use the static runtime with a DLL, or the dynamic runtime with a static library.

---

<div class="post-metadata">

### Author: ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)
#### Post date: [August 21, 2022, 10:44am UTC](https://discourse.cmake.org/t/link-external-static-lib-along-with-dynamic-libs-to-the-executable/6309/5 "2022-08-21T10:44:51Z")

</div>

Thank you! I will try to google that.  
I think I have lack of knowledge there.
