Configure cmake project with VS code in WSL

It is quite nice to use Visual Code as an IDE to build linux cpp projects in a WSL environment.

  1. Install WSL2 or WSL

2. Install VS code in Win 10 and install the neccessary plugins, including Remote WSL, C/C++, C++ Intellisense and Cmake. 

3. In WSL, go to the director where you want to make a cpp project and open VS code in current folder by “code .” Then, a VS code window linked with WSL is opened.

4. Create a new cpp file in this folder.

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {“Hello”, “C++”, “World”, “from”, “VS Code”, “and the C++ extension!”};
for (const string& word : msg)
{
cout << word << ” “;
}
cout << endl;
}

5. Create a file named CMakeLists.txt.

cmake_minimum_required(VERSION 2.8)
project(test)
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
add_executable(main main.cpp)
target_link_libraries (main Eigen3::Eigen)

6. From the main menu, choose “Terminal> Configure default build task”. In the dropdown, which will display a tasks dropdown listing various predefined build tasks for C++ compilers. Choose “g++ build active file”, which will build the file that is currently displayed (active) in the editor. Change the “tasks.jason”. Make a new folder named “Build”. With the “tasks.jason” file, it possible to build the project with cmake by press Ctrl+Shift+b.

{   “version”: “2.0.0”,
    “tasks”: [
        {
            “type”: “shell”,
            “label”: “build-cmake”,
            “command”: “cd build; rm -rf *; cmake ..; make”,
            “group”: {
                “kind”: “build”,
                “isDefault”: true
            },
            “problemMatcher”: [
                “$gcc”
            ]
        },
        {
            “type”: “shell”,
            “label”: “build-debug-cmake”,
            “command”: “cd build;  rm -rf *; cmake -DCMAKE_BUILD_TYPE=Debug ..; make”,
            “group”: {
                “kind”: “build”,
                “isDefault”: true
            }
        }
    ]
}

7. From the main menu, choose “Run > Add configuration”. Choose “g++ build and debug active file”. Edit it to configure the debug setting. Then we can debug the project by pressing “F5”. If you do not want to stop before the first line is executed during debugging, you can change the setting from “”stopAtEntry”: true” to “”stopAtEntry”: false”.

{
    “version”: “0.2.0”,
    “configurations”: [
        {
            “name”: “debug-cmake”,
            “type”: “cppdbg”,
            “request”: “launch”,
            “program”: “${fileDirname}/build/${fileBasenameNoExtension}”,
            “args”: [],
            “stopAtEntry”: true,
            “cwd”: “${workspaceFolder}”,
            “environment”: [],
            “externalConsole”: false,
            “MIMode”: “gdb”,
            “setupCommands”: [
                {
                    “description”: “Enable pretty-printing for gdb”,
                    “text”: “-enable-pretty-printing”,
                    “ignoreFailures”: true
                }
            ],
            “preLaunchTask”: “build-debug-cmake”,
            “miDebuggerPath”: “/usr/bin/gdb”
        }
    ]
}

8. It is also possible to configure a cmake project with the plugin “CMake Tools” by following “CMake-quick start”  -> “CMake-Build” or “CMake-Debug”.

Posted in Uncategorized | Tagged , , | Leave a comment

Installation of WSL and WSL2

WSL is Windows Subsystem for Linux.

 

The installation of WSL can be found https://docs.microsoft.com/nl-nl/windows/wsl/install-win10 .

 

WSL can be upgraded to WSL2  for a better file I/O performance. The upgrdation instruction can be found https://docs.microsoft.com/en-us/windows/wsl/wsl2-install.

[youtube]https://www.youtube.com/watch?v=ilKQHAFeQR0[/youtube]

Note: make sure that the vitrualization support in BIOS is on when upgrading WSL to WSL2.

Posted in Uncategorized | Tagged , | Leave a comment