title: Generate Static Executable with CMake
body: <p>Please bear with this question, it is rather long.</p>
  <p><strong>TLDR: A CMake project with a subdirectory library links successfully, but creates a dynamic executable.</strong></p>
  <p>Code Available at: <a href="https://github.com/georcon/cmake-issue" rel="nofollow noreferrer">https://github.com/georcon/cmake-issue</a></p>
  <p>Also <strong>Note:</strong> I have read all related questions/answers, and none answer this question.</p>
  <p>I have created the following minimal example:</p>
  <h2>Create a statically-linked executable (Works correctly)</h2>
  <p>(Git Tag: SimpleExecutable)</p>
  <p><strong>main.c</strong></p>
  <pre><code>#include &lt;uuid/uuid.h&gt;
  #include &lt;stdio.h&gt;
  
  
  int main(){
          uuid_t some_uuid;
          char uuid_str[40];
  
          uuid_generate(some_uuid);
          uuid_unparse(some_uuid, uuid_str);
  
          printf(&quot;UUID: %s\n&quot;, uuid_str);
          return 0;
  }
  </code></pre>
  <p><strong>CMakeLists.txt</strong></p>
  <pre><code>
  project(cmake-issue VERSION 1.0 DESCRIPTION &quot;Static target issue&quot; LANGUAGES C)
  
  add_executable(main-dynamic main.c)
  target_link_libraries(main-dynamic uuid)
  
  add_executable(main-static main.c)
  target_link_libraries(main-static uuid -static)
  </code></pre>
  <p><strong>Result</strong></p>
  <pre><code>ldd main-dynamic
  linux-vdso.so.1 (0x00007ffc406bb000)
  libuuid.so.1 =&gt; /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007f76781cd000)
  libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7677fdb000)
  /lib64/ld-linux-x86-64.so.2 (0x00007f76781eb000)
  
  ldd main-static
  not a dynamic executable
  </code></pre>
  <h2>Create a static executable with library</h2>
  <p>(Git Tag: ExecutableWithLibrary)</p>
  <p><strong>lib/lib.h</strong></p>
  <pre><code>#ifndef LIB_H
  #define LIB_H
  
  void PrintUUID();
  
  #endif //LIB_H
  </code></pre>
  <p><strong>lib/lib.c</strong></p>
  <pre><code>#include &lt;uuid/uuid.h&gt;
  #include &lt;stdio.h&gt;
  
  void PrintUUID(){
  
          uuid_t some_uuid;
          char uuid_str[40];
  
          uuid_generate(some_uuid);
          uuid_unparse(some_uuid, uuid_str);
  
          printf(&quot;UUID: %s\n&quot;, uuid_str);
  }
  </code></pre>
  <p><strong>lib/CMakeLists.txt</strong></p>
  <pre><code>cmake_minimum_required(VERSION 3.13)
  
  project(testlibrary VERSION 1.0 DESCRIPTION &quot;Static target issue - Library&quot; LANGUAGES C)
  
  add_library(testlib lib.c)
  target_link_libraries(testlib uuid)
  </code></pre>
  <p><strong>main.c</strong></p>
  <pre><code>#include &quot;lib/lib.h&quot;
  
  int main(){
          PrintUUID();
          return 0;
  }
  </code></pre>
  <p><strong>CMakeLists.txt</strong></p>
  <pre><code>cmake_minimum_required(VERSION 3.13)
  
  project(cmake-issue VERSION 1.0 DESCRIPTION &quot;Static target issue&quot; LANGUAGES C)
  
  add_subdirectory(lib ./bin)
  link_directories(./lib/ ./bin)
  
  add_executable(main-dynamic main.c)
  add_dependencies(main-dynamic testlib)
  target_link_libraries(main-dynamic libtestlib.a uuid -static)
  
  link_libraries(&quot;-static&quot;)
  
  add_executable(main-static main.c)
  target_link_libraries(main-static PUBLIC &quot;-static&quot; libtestlib.a uuid)
  add_dependencies(main-static testlib)
  #target_link_libraries(main-static libtestlib.a uuid -static)
  </code></pre>
  <p><strong>Result</strong></p>
  <pre><code>ldd main-static
          linux-vdso.so.1 (0x00007ffe6b485000)
          libuuid.so.1 =&gt; /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007fc67edd3000)
          libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc67ebe1000)
          /lib/ld64.so.1 =&gt; /lib64/ld-linux-x86-64.so.2 (0x00007fc67edec000)
  </code></pre>
  <p>Looking at the linker command:</p>
  <blockquote>
  <p>~/cmake_issue$ cat CMakeFiles/main-static.dir/link.txt /usr/bin/cc<br />
  CMakeFiles/main-static.dir/main.c.o  -o main-static<br />
  -L/home/georcon/cmake_issue/./lib  -L/home/georcon/cmake_issue/./bin  -Wl,-rpath,/home/georcon/cmake_issue/./lib:/home/georcon/cmake_issue/./bin -static -static
  -Wl,-Bstatic -ltestlib -Wl,<strong>-Bdynamic</strong> -luuid</p>
  </blockquote>
  <p><strong>Why does CMake not generate a statically-linked executable in this case?</strong></p>