# Is there any command line option to force static build?

**URL:** https://discourse.cmake.org/t/is-there-any-command-line-option-to-force-static-build/14336
**Category:** Usage
**Tags:** os:linux, gen:makefiles, gen:ninja
**Created:** [July 9, 2025, 5:56pm UTC](https://discourse.cmake.org/t/is-there-any-command-line-option-to-force-static-build/14336 "2025-07-09T17:56:49Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![leo9800](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leo9800/32/5660_2.png) [@leo9800](https://discourse.cmake.org/u/leo9800)
#### Post date: [July 9, 2025, 5:56pm UTC](https://discourse.cmake.org/t/is-there-any-command-line-option-to-force-static-build/14336/1 "2025-07-09T17:56:49Z")

</div>

Hi, folks

I am currently trying to build static binary of a C++ & CMake based project, [qbittorrent-enhanced-edition](https://github.com/c0re100/qBittorrent-Enhanced-Edition) to be specific.

My goal would be producing a ELF which is completely static, i.e. `ldd` shows `statically linked`.

all static libraries required are in there position, i.e. `/usr/lib`:

```auto
07ab00ba483e:/build/qBittorrent-Enhanced-Edition/build# ls /usr/lib/*.a
/usr/lib/libQt6DeviceDiscoverySupport.a /usr/lib/libboost_regex.a
/usr/lib/libQt6ExampleIcons.a /usr/lib/libboost_serialization.a
/usr/lib/libQt6ExamplesAssetDownloader.a /usr/lib/libboost_stacktrace_addr2line.a
/usr/lib/libQt6FbSupport.a /usr/lib/libboost_stacktrace_basic.a
/usr/lib/libQt6InputSupport.a /usr/lib/libboost_stacktrace_noop.a
/usr/lib/libQt6KmsSupport.a /usr/lib/libboost_system.a
/usr/lib/libQt6PacketProtocol.a /usr/lib/libboost_test_exec_monitor.a
/usr/lib/libQt6QmlAssetDownloader.a /usr/lib/libboost_thread.a
/usr/lib/libQt6QmlDebug.a /usr/lib/libboost_timer.a
/usr/lib/libQt6QmlDom.a /usr/lib/libboost_type_erasure.a
/usr/lib/libQt6QmlLS.a /usr/lib/libboost_unit_test_framework.a
/usr/lib/libQt6QmlToolingSettings.a /usr/lib/libboost_url.a
/usr/lib/libQt6QmlTypeRegistrar.a /usr/lib/libboost_wave.a
/usr/lib/libQt6QuickControlsTestUtils.a /usr/lib/libboost_wserialization.a
/usr/lib/libQt6QuickTestUtils.a /usr/lib/libc.a
/usr/lib/libXcomposite.a /usr/lib/libcrypt.a
/usr/lib/libXcursor.a /usr/lib/libcrypto.a

# truncated

```

I had tried `Ninja` and `Unix Makefiles` generator with various possible cli options I browsed from web incl:

- `DCMAKE_{EXE|MODULE|SHARED|STATIC}_LINKER_FLAGS="-static-libgcc -static-libstdc++ -static"`
- `-DCMAKE_FIND_LIBRARY_SUFFIXES=".a"`
- `-DBUILD_SHARED_LIBS=OFF`

but CMake just stubbornly insists on linking against the shared libraries, not the static ones, resulting in such error when linking:

```auto
[99%] Linking CXX executable ../../qbittorrent-nox
/usr/lib/gcc/x86_64-alpine-linux-musl/14.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: attempted static link of dynamic object `/usr/lib/libcrypto.so'
collect2: error: ld returned 1 exit status
make[2]: *** [src/app/CMakeFiles/qbt_app.dir/build.make:1758: qbittorrent-nox] Error 1
make[1]: *** [CMakeFiles/Makefile2:255: src/app/CMakeFiles/qbt_app.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

```

is there any option available to force cmake to link against static libraries only and disregard any shared libraries?

best regards.
