title: How to build executable and shared libraries in same cmake project on windows/msvs
body: <p>what I want to do is :</p>
  
  <ul>
  <li>create a shared library and export its API to be usable by other programs</li>
  <li>create a simple executable in the same project that uses the library (to show concrete example of how to use the library for example</li>
  <li>build it using cmake, must work with Visual Studio (2010) and windows 7</li>
  </ul>
  
  <p>I tried this code (quick test case) :</p>
  
  <p>CMakeLists.txt</p>
  
  <pre><code>PROJECT (minimalcpp)
  CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
  
  SET(LIBSAMPLE_HEADERS func_simple.h)
  SET(LIBSAMPLE_SRCS func_simple.cpp)
  
  ADD_LIBRARY (minimalcpp SHARED ${LIBSAMPLE_SRCS})
  
  ADD_EXECUTABLE (test-pure-cpp test-pure-cpp.cpp)
  TARGET_LINK_LIBRARIES (test-pure-cpp minimalcpp)
  
  # THIS WORKS BUT IT IS NOT WHAT I WANT :
  # ADD_EXECUTABLE (test-pure-cpp test-pure-cpp.cpp ${LIBSAMPLE_SRCS})
  </code></pre>
  
  <p>macros.h</p>
  
  <pre><code>#ifndef MACROS_H
  #define MACROS_H
  
  #if defined _WIN32 || defined __CYGWIN__
      #if minimalcpp_EXPORTS
          #define MINIMALCPP_API __declspec(dllexport)
      #else
          #define MINIMALCPP_API __declspec(dllimport)
      #endif
  #endif
  
  #endif
  </code></pre>
  
  <p>func_simple.h</p>
  
  <pre><code>#ifndef LIB_SAMPLE_FUNC_SIMPLE_H
  #define LIB_SAMPLE_FUNC_SIMPLE_H
  
  #include "macros.h"
  
  namespace sample {
  MINIMALCPP_API void f1(int nmax);
  }
  
  #endif // LIB_SAMPLE_FUNC_SIMPLE_H
  </code></pre>
  
  <p>func_simple.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "func_simple.h"
  
  void sample::f1(int nmax) {
    int i ;
    for(i=0 ; i &lt; nmax ; i++) {
      std::cout &lt;&lt; i &lt;&lt; " -&gt; " &lt;&lt; i*i &lt;&lt; std::endl;
    }
  }
  </code></pre>
  
  <p>test-pure-cpp.cpp</p>
  
  <pre><code>#include "func_simple.h"
  
  int main(int argc, char *argv[]){
      sample::f1(5);
      return 0;
  }
  </code></pre>
  
  <p>This code compiles but crashes directly at execution.</p>
  
  <p>Error message :</p>
  
  <pre><code>Le programme s'est terminé subitement.
  ... a quitté avec le code -1073741515
  </code></pre>
  
  <p>I am a beginner in C++ on windows, what did I do wrong ?
  Thank you very much</p>