# Static executable on linux using latest CMake

**URL:** https://discourse.cmake.org/t/static-executable-on-linux-using-latest-cmake/6872
**Category:** Usage
**Created:** [November 15, 2022, 10:22am UTC](https://discourse.cmake.org/t/static-executable-on-linux-using-latest-cmake/6872 "2022-11-15T10:22:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Glad](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@Glad](https://discourse.cmake.org/u/Glad)
#### Post date: [November 15, 2022, 10:22am UTC](https://discourse.cmake.org/t/static-executable-on-linux-using-latest-cmake/6872/1 "2022-11-15T10:22:38Z")

</div>

Hi,  
I am using CMake 3.24.3.  
What is the cleanest way, most modern way, to mandate a static executable?

I want to make sure that my final binary will be able to run on remote Linux servers, without worrying about the host setup, making sure that the executable will run exactly as on my system.  
No more guessing.

Thanks,

---

<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: [November 15, 2022, 7:56pm UTC](https://discourse.cmake.org/t/static-executable-on-linux-using-latest-cmake/6872/2 "2022-11-15T19:56:31Z")

</div>

The easiest way is to tell all dependencies to build statically. You’ll also probably want to use `musl` instead of `glibc`. For that, I would recommend building with Alpine or a similar distribution myself.

---

<div class="post-metadata">

### Author: ![Glad](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@Glad](https://discourse.cmake.org/u/Glad)
#### Post date: [November 15, 2022, 9:15pm UTC](https://discourse.cmake.org/t/static-executable-on-linux-using-latest-cmake/6872/3 "2022-11-15T21:15:42Z")

</div>

Thanks Ben,  
building statically, yes, but I would like the help of Cmake as much as I can not to miss a detail.

Cmake doesn’t have a one liner of the like “CMAKE\_LIBRARIES(“static”)” that would force the linker to choose .a and not .so?

---

<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: [November 15, 2022, 9:49pm UTC](https://discourse.cmake.org/t/static-executable-on-linux-using-latest-cmake/6872/4 "2022-11-15T21:49:29Z")

</div>

No, there’s no real way to do that AFAIK. Every `find_package` (that supports this) would usually have their own mechanism for selecting static versus shared. You might be able to fiddle with [`CMAKE_FIND_LIBRARY_SUFFIXES`](https://cmake.org/cmake/help/latest/variable/CMAKE_FIND_LIBRARY_SUFFIXES.html) to only let `find_library` find static libraries, but projects that use exported targets don’t use `find_library`. I think I’d add a test or something that checks whether your binary is a static executable or not at least. If you use an `add_custom_command` it’d run during the build, but `add_test` can work too.

---

<div class="post-metadata">

### Author: ![Glad](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@Glad](https://discourse.cmake.org/u/Glad)
#### Post date: [November 15, 2022, 9:51pm UTC](https://discourse.cmake.org/t/static-executable-on-linux-using-latest-cmake/6872/5 "2022-11-15T21:51:43Z")

</div>

Yes,

I think Appimage is what I am after I guess.  
It would overcome all the difficulties.
