# Static runtime library

**URL:** <https://discourse.cmake.org/t/static-runtime-library/12721>\
**Category:** Code\
**Tags:** os:windows\
**Created:** [October 8, 2024, 6:48pm UTC](https://discourse.cmake.org/t/static-runtime-library/12721 "2024-10-08T18:48:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilyaplay2509](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ilyaplay2509/32/5006_2.png) [@ilyaplay2509](https://discourse.cmake.org/u/ilyaplay2509)\
**Post date:** [October 8, 2024, 6:48pm UTC](https://discourse.cmake.org/t/static-runtime-library/12721/1 "2024-10-08T18:48:03Z")

</div>

I need to compile executable with static runtime library for compilers msvc, gcc, clang. For msvc I do and it completely work

```auto
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
        set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
        set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")

```

For mingw gcc I do

```auto
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -static-libgcc -static-libstdc++"

```

And with this I still have dependency of msvcrt.dll  
For clang I do

```auto
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static"

```

With this I still have a lot of dependencies. I tried to remove flag -D\_DLL and it didn’t worked

So what is the best solution to get rid of all dependencies?

---

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [October 16, 2024, 2:57pm UTC](https://discourse.cmake.org/t/static-runtime-library/12721/2 "2024-10-16T14:57:26Z")

</div>

I don’t know if it will resolve your problem of dependency (should be related to used generator, I guess), but uoutd use `add_compile_options()`:

```auto
add_compile_options($<$<CXX_COMPILER_ID:MSVC>:/MP>)
add_compile_options($<$<CXX_COMPILER_ID:MSVC>:/MTd>)

```

---

<div class="post-metadata">

**Author:** ![Lecris](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lecris/32/3193_2.png) [@Lecris](https://discourse.cmake.org/u/Lecris)\
**Post date:** [October 18, 2024, 6:50am UTC](https://discourse.cmake.org/t/static-runtime-library/12721/3 "2024-10-18T06:50:32Z")

</div>

Ideally you have the targets defined as `STATIC` in any of the `find_package`, `find_library`, `add_library`, then it should be automatically handled when you use `target_link_libraries`. If that is nit the case, it seems like a bug in the language definition section for the specific compiler.
