(Windows) Install and Configure VSCode with CMake - kurogane1031/itolab_weekly_cpp GitHub Wiki

Installing Visual Studio Code

  1. Download VSCode installer. Once finished, run the executable.
  2. When prompt Select Additional Tasks, select the following options
    • Add "Open with Code" action to Windows Explorer
    • Add "Open with Code" action to Windows Explorer directory context Menu
    • Register Code as an editor for supported file types
    • Add to PATH (requires shell restart) Select Additional Tasks
  3. Select Next to complete installation.

Install Extensions

We need to install 3 extensions: C/C++, CMake, CMake Tools.

  1. Open VSCode.
  2. On the left sidebar, click on Extensions Icon (keybinding: Ctrl+Shift+x).
  3. Search for C/C++ extension by Microsoft. Click on Install. Extension C/C++
  4. Repeat the same for CMake and CMake Tools. Extension CMake Extension CMake Tools
  5. For completion purpose, close and reopen VSCode.

Configuring CMake's User settings

  1. Open VSCode
  2. Go to File, Preferences and Settings (keybinding: Ctrl+,).
  3. In Settings tab, under User, select Extensions and CMake configuration. Settings Tab
  4. On the Cmake Path, type the location of cmake.exe. By default is C:\Program Files\CMake\bin\cmake.exe.
  5. Next under CMake Tools configuration, find Cmake: Cmake Path and again, make sure the location of cmake.exe. It should be similar to the one in CMake configuration.
  6. Just to make sure thing is okay, open settings.json. You can do this by Ctrl+Shift+P and search Preference: Open Settings (JSON). At the very least, your settings.json` should look as follow.
{
    "cmake.cmakePath": "C:\\Program Files\\CMake\\bin\\cmake.exe",
    "cmake.copyCompileCommands": "${workspaceFolder}",
    "cmake.generator": "MinGW Makefiles",
    "C_Cpp.updateChannel": "Insiders",
    "cmake.configureOnOpen": true,
    "C_Cpp.default.compilerPath": "C:\\MinGW\\bin",
}

If one of them is missing, you can just copy and paste them.

Testing CMake

  1. Close any open folder.

  2. (Assuming you cloned this repository) Open Week02 folder.

  3. You may be prompt to select the compiler. However, if you accidentally lost the selection, type Ctrl+Shift+P, search and select CMake: Configure.

    3.1 Open CMakeLists.txt, and comment out the following

    # SET(CMAKE_CXX_COMPILER "/usr/bin/clang++-10")
    # SET(CMAKE_CXX_FLAGS "-g -Wall -Wno-conversion -Wextra -Wpedantic -ftime-trace")
    

    CMakeLists.txt

  4. If the OUTPUT console is as follows, it means you're successful.

[main] Configuring folder: week02 
[main] Configuring folder: week02 
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.exe" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\MinGW\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\MinGW\bin\g++.exe "-Hd:/Google Drive (Shibaura)/New folder/itolab_weekly_cpp/itolab_weekly_cpp/week02" "-Bd:/Google Drive (Shibaura)/New folder/itolab_weekly_cpp/itolab_weekly_cpp/week02/build" -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 9.2.0
[cmake] -- The CXX compiler identification is GNU 9.2.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: C:/MinGW/bin/gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: C:/MinGW/bin/g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to:
  1. Now go to the TERMINAL, type cd build the followed by cmake ..
  2. To compile the program, type mingw32-make.
  3. Done!