title: Link libc statically
body: <p>I am trying to make a static executable with CMake 3.15. I am building on Alpine Linux (hence with musl), and currently, my executable's ldd output is:</p>
  
  <pre><code># ldd my_executable
      /lib/ld-musl-x86_64.so.1 (0x7fc6f7977000)
      libstdc++.so.6 =&gt; /usr/lib/libstdc++.so.6 (0x7fc6f65b3000)
      libc.musl-x86_64.so.1 =&gt; /lib/ld-musl-x86_64.so.1 (0x7fc6f7977000)
      libgcc_s.so.1 =&gt; /usr/lib/libgcc_s.so.1 (0x7fc6f659f000)
  </code></pre>
  
  <p>I can set <code>target_link_options(my_executable PRIVATE -static-libgcc -static-libstdc++)</code>, and they got linked statically:</p>
  
  <pre><code># ldd my_executable
      /lib/ld-musl-x86_64.so.1 (0x7fc6f7977000)
      libc.musl-x86_64.so.1 =&gt; /lib/ld-musl-x86_64.so.1 (0x7fc6f7977000)
  </code></pre>
  
  <p>But I can't manage to get musl to link statically. I tried (with clean builds, to make sure that the configure step starts from zero):</p>
  
  <ol>
  <li>Adding <code>-static</code> to <code>target_link_options</code> above.</li>
  <li><code>set(CMAKE_EXE_LINKER_FLAGS "-static")</code> before the call to <code>add_executable(my_executable ...)</code></li>
  <li>Adding <code>-static</code> to <code>target_link_libraries(my_executable lib1 lib2 -static)</code></li>
  </ol>
  
  <p>When I run CMake in <code>VERBOSE=1</code> mode, it always ends the linking command with:</p>
  
  <pre><code>... -Wl,-Bdynamic -ldl -lrt -lm -lpthread
  </code></pre>
  
  <p>I believe that this is my issue: I want to get rid of that <code>-Bdynamic</code>. What am I missing? Is this <code>-Bdynamic</code> coming from one of my dependencies? I build them all from sources as static (<code>.a</code>) libraries, so how could they be linking libc dynamically? Or would I need to patch them all to add <code>-static</code> when I build them?</p>