title: How to link a library (SDL 2) to C++ project using VSCode and CMake (Windows)
body: <p>Here is the thing: I want to use SDL for a project, but being new to creating c++ projects from scratch, there is no way to link this library to my project. This is caused by my lack of knowledge about CMake and the tasks.json and CMakeLists.txt files.</p>
  
  <p>What I have right now, after several hours of googling</p>
  
  <p>The main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "SDL/inc/SDL.h"
  
  int main() {
      const int image_width = 200;
      const int image_height = 100;
  
      std::cout &lt;&lt; "P3\n" &lt;&lt; image_width &lt;&lt; ' ' &lt;&lt; image_height &lt;&lt; "\n255\n";
  
      for (int j = image_height-1; j &gt;= 0; --j) {
          std::cerr &lt;&lt; "\rScanlines remaining: " &lt;&lt; j &lt;&lt; ' ' &lt;&lt; std::flush;
          for (int i = 0; i &lt; image_width; ++i) {
              auto r = double(i) / image_width;
              auto g = double(j) / image_height;
              auto b = 0.2;
              int ir = static_cast&lt;int&gt;(255.999 * r);
              int ig = static_cast&lt;int&gt;(255.999 * g);
              int ib = static_cast&lt;int&gt;(255.999 * b);
              std::cout &lt;&lt; ir &lt;&lt; ' ' &lt;&lt; ig &lt;&lt; ' ' &lt;&lt; ib &lt;&lt; '\n';
          }
      }
      std::cerr &lt;&lt; "\nDone.\n";
  }
  </code></pre>
  
  <p>CMakeLists.txt:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.0)
  
  project(ArchTracer)
  
  set(SOURCE main.cpp)
  
  include_directories("${PROJECT_SOURCE_DIR}/SDL")
  add_subdirectory(SDL)
  
  add_executable(${PROJECT_NAME} ${SOURCE})
  target_link_libraries(${PROJECT_NAME} SDL2)
  </code></pre>
  
  <p>tasks.json:</p>
  
  <pre><code>{
      // See https://go.microsoft.com/fwlink/?LinkId=733558
      // for the documentation about the tasks.json format
      "version": "2.0.0",
      "SuppressTaskName": true,
      "type": "shell",
      "options": {
          "cwd": "${workspaceRoot}/build"
      },
      "tasks": [
          {
              "label": "cmake",
              "command": ["cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug .."]
          },
          {
              "label": "make",
              "command": ["make -j 8tasks.json"],
              "group": {
                  "kind": "build",
                  "isDefault": true,
              }
          }
      ]
  }
  </code></pre>
  
  <p>The c_cpp_properties.json:</p>
  
  <pre><code>{
      "configurations": [
          {
              "name": "Win32",
              "includePath": [
                  "${workspaceFolder}/**",
                  "${workspaceFolder}/SDL/inc",
                  "${workspaceFolder}/SDL/lib/x64"
              ],
              "defines": [
                  "_DEBUG",
                  "UNICODE",
                  "_UNICODE"
              ],
              "windowsSdkVersion": "10.0.18362.0",
              "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe",
              "cStandard": "c11",
              "cppStandard": "c++17",
              "intelliSenseMode": "msvc-x64"
          }
      ],
      "version": 4
  }
  </code></pre>
  
  <p>and finally the CMakeList INSIDE the SDL folder, which is in the project root folder, where the .cpp is:</p>
  
  <pre><code>message("-- Linking SDL")
  add_library(SDL2 SDL2.dll)
  set_target_properties(SDL2 PROPERTIES LINKER_LANGUAGE CXX)
  </code></pre>
  
  <p>(The code was compiling fine before that library stuff)</p>
  
  <p>This is what the compiler is saying:</p>
  
  <pre><code>make[1]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
  make[2]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
  make[2]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
  [ 33%] Built target SDL2
  make[2]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
  make[2]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
  make[2]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
  [ 66%] Linking CXX executable ArchTracer.exe
  /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/libcygwin.a(libcmain.o): in function `main':
  /usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
  /usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/lib/libcmain.c:37:(.text.startup+0x7f): relocation truncated to fit: R_X86_64_PC32 against 
  undefined symbol `WinMain'
  collect2: error: ld returned 1 exit status
  make[2]: *** [CMakeFiles/ArchTracer.dir/build.make:85: ArchTracer.exe] Error 1
  make[2]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
  make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/ArchTracer.dir/all] Error 2
  make[1]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
  make: *** [Makefile:84: all] Error 2
  The terminal process terminated with exit code: 1
  </code></pre>
  
  <p>I would like to understand how to tell cmake where are the include files and where is the lib.
  I already told VSCode where to find these.</p>
  
  <p>Does someone can help with that ?</p>
  
  <p>Thank you very much :)</p>