# how can i build static program by cmake+clang+nmake on windows?

**URL:** https://discourse.cmake.org/t/how-can-i-build-static-program-by-cmake-clang-nmake-on-windows/9226
**Category:** Code
**Tags:** os:windows
**Created:** [October 20, 2023, 4:02am UTC](https://discourse.cmake.org/t/how-can-i-build-static-program-by-cmake-clang-nmake-on-windows/9226 "2023-10-20T04:02:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![KSbread](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ksbread/32/3927_2.png) [@KSbread](https://discourse.cmake.org/u/KSbread)
#### Post date: [October 20, 2023, 4:02am UTC](https://discourse.cmake.org/t/how-can-i-build-static-program-by-cmake-clang-nmake-on-windows/9226/1 "2023-10-20T04:02:25Z")

</div>

i asked this question on stackoverflow two days ago.this is [original post](https://stackoverflow.com/questions/77316727/how-can-i-build-static-program-by-cmakeclang-on-windows).  
due to my low english proficiency,please don’t be surprised if there are any strange expressions.  
thanks for your response.

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [October 20, 2023, 7:44am UTC](https://discourse.cmake.org/t/how-can-i-build-static-program-by-cmake-clang-nmake-on-windows/9226/2 "2023-10-20T07:44:18Z")

</div>

So you want your executable to statically link to CRT (_C++ runtime_)? Because the term “static program” is probably not the most correct one.

You can take a look at how Microsoft itself does it in their [vcpkg toolchain](https://github.com/microsoft/vcpkg/blob/master/scripts/toolchains/windows.cmake#L44C13-L44C39). So in your project it would be something like this (_note the leading space inside quotes_):

```cmake
string(APPEND CMAKE_C_FLAGS_DEBUG " /MT")
string(APPEND CMAKE_C_FLAGS_RELEASE " /MT")

string(APPEND CMAKE_CXX_FLAGS_DEBUG " /MT")
string(APPEND CMAKE_CXX_FLAGS_RELEASE " /MT")

```

But for that to work you might need to use Visual Studio generator (_I’ve never used NMake Makefiles_).

---

<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: [October 20, 2023, 3:36pm UTC](https://discourse.cmake.org/t/how-can-i-build-static-program-by-cmake-clang-nmake-on-windows/9226/3 "2023-10-20T15:36:37Z")

</div>

This approach is quite obsolete.  
Use preferably the variable [CMAKE\_MSVC\_RUNTIME\_LIBRARY](https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html) or the target property [MSVC\_RUNTIME\_LIBRARY](https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html#prop_tgt:MSVC_RUNTIME_LIBRARY) to control how the build is done.

---

<div class="post-metadata">

### Author: ![KSbread](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ksbread/32/3927_2.png) [@KSbread](https://discourse.cmake.org/u/KSbread)
#### Post date: [October 21, 2023, 3:51pm UTC](https://discourse.cmake.org/t/how-can-i-build-static-program-by-cmake-clang-nmake-on-windows/9226/4 "2023-10-21T15:51:10Z")

</div>

i’ve tried these two variables but seems not work. Could you please give me an example?  
actually, i’ve solved this weird problem by use this:

```auto
string(REPLACE "--dependent-lib=msvcrtd" "--dependent-lib=libcmtd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
string(REPLACE "-D_DLL" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})

```

and the whole CMakeLists.txt is:

```auto
cmake_minimum_required(VERSION 3.5)

set(CMAKE_C_COMPILER "C:/Software/LLVM/bin/clang.exe")
set(CMAKE_CXX_COMPILER "C:/Software/LLVM/bin/clang++.exe")

project(helloworld)

set(CMAKE_VERBOSE_MAKEFILE ON)

string(REPLACE "--dependent-lib=msvcrtd" "--dependent-lib=libcmtd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
string(REPLACE "-D_DLL" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})

message(${CMAKE_CXX_FLAGS_DEBUG})

set(CMAKE_CXX_COMPILER_TARGET i686-pc-windows-msvc)

set(CMAKE_CXX_STANDARD 17)
set(SOURCE helloworld.cpp)
add_executable(${PROJECT_NAME} ${SOURCE})

```

may this is a stupid solution,I don’t know how to do it better…  
please reply me if you have any better suggestions, thank you very much.

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [October 21, 2023, 3:54pm UTC](https://discourse.cmake.org/t/how-can-i-build-static-program-by-cmake-clang-nmake-on-windows/9226/5 "2023-10-21T15:54:20Z")

</div>

I see neither my/Microsoft’s suggestion about `/MT` nor Marc’s suggestion about `MSVC_RUNTIME_LIBRARY` (_much better one, admittedly_) in your project file fragment.

---

<div class="post-metadata">

### Author: ![KSbread](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ksbread/32/3927_2.png) [@KSbread](https://discourse.cmake.org/u/KSbread)
#### Post date: [October 21, 2023, 4:23pm UTC](https://discourse.cmake.org/t/how-can-i-build-static-program-by-cmake-clang-nmake-on-windows/9226/6 "2023-10-21T16:23:07Z")

</div>

i tried both of your suggestions but seems clang doesn’t support `MSVC_RUNTIME_LIBRARY `(just ignore and nothing happened) and `/MT`(it did good job in Visual Studio but i prefer not to use visual studio but vscode)… therefore, i replace the flags manually and delete those code…

---

<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: [October 21, 2023, 5:39pm UTC](https://discourse.cmake.org/t/how-can-i-build-static-program-by-cmake-clang-nmake-on-windows/9226/7 "2023-10-21T17:39:19Z")

</div>

The following is perfectly operational but require, at least, CMake version 3.15 (you specified version 3.5 through `cmake_minimum_required()` command):

```cmake
cmake_minimum_required(VERSION 3.15)

set(CMAKE_C_COMPILER "C:/Software/LLVM/bin/clang.exe")
set(CMAKE_CXX_COMPILER "C:/Software/LLVM/bin/clang++.exe")

project(helloworld)

set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_CXX_STANDARD 17)
set(SOURCE helloworld.cpp)
add_executable(${PROJECT_NAME} ${SOURCE})
set_property(TARGET main PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebug)

```

By the way, I strongly suggest to you to read the [CMake documentation](https://cmake.org/cmake/help/latest/index.html). For example, setting variable `CMAKE_CXX_COMPILER_TARGET` outside a toolchain file is irrelevant.

---

<div class="post-metadata">

### Author: ![KSbread](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ksbread/32/3927_2.png) [@KSbread](https://discourse.cmake.org/u/KSbread)
#### Post date: [October 22, 2023, 12:45pm UTC](https://discourse.cmake.org/t/how-can-i-build-static-program-by-cmake-clang-nmake-on-windows/9226/8 "2023-10-22T12:45:31Z")

</div>

thanks for your perfect solution, i’ll try to read cmake doc.
