title: CMake link shared library on Windows
body: <p>There are three files, (<em>m.c</em>,<em>m.h</em>, and **main.c*).</p>
  
  <h3>File <em>m.h</em></h3>
  
  <pre><code>// m.h
  int m();
  </code></pre>
  
  <h3>File <em>m.c</em></h3>
  
  <pre><code>// m.c
  #include &lt;stdio.h&gt;
  #include "m.h"
  
  int m(){
      printf("Hello,m!\n");
      return 0;
  }
  </code></pre>
  
  <h3>File <em>main.c</em></h3>
  
  <pre><code>// main.c
  #include "m.h"
  int main(){
      return m();
  }
  </code></pre>
  
  <p>While I prefer a shared library (<em>m.dll</em>), I've made the <a href="https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#organization" rel="noreferrer">CMakeLists.txt</a> file:</p>
  
  <pre><code>    PROJECT("app1")
      ADD_LIBRARY(m SHARED m.c)
      ADD_EXECUTABLE(myexe main.c)
      TARGET_LINK_LIBRARIES(myexe m)
  </code></pre>
  
  <p>The CMake configuration is done and generated done. Opening app1.sln and building with Visual Studio, it crashes as</p>
  
  <pre><code>LNK1104:Can't open file "Debug\m.lib"
  </code></pre>
  
  <p>It only works as <strong>STATIC</strong> at <code>ADD_LIBRARY()</code>. Why doesn't it work on Windows?</p>
  
  <p>If I got another shared library (<em>mylib.dll</em>), how could I invoke its functions in my main.c and CMakeLists.txt files?</p>