How to configure Visual Studio Code for C/C++

Visual studio code is a good IDE for code developer, e.g. I am previously coding C++/C using visual studio in Window. But after I move into linux/mac os, I need to find a IDE (free). If you are a geek, maybe you like VI. But for me, I am familiar with IDE. Visual studio code is a replacement of Visual Studio, and free.

Before coding, compiling, debugging and running your code, you need to do some setup. Following is step-by-step for C++/C

Step 1: Install Visual studio code

Download https://code.visualstudio.com/, and install. Install C/C++ plugin. e.g.

C/C++ IntelliSense, debugging, and code browsing.

Step 2: Create a test file, e.g.
test.cpp

#include <iostream>

using namespace std;

int main() {
    int i;

    cin >> i;
    cout << "**** \n" << i << "\n";

    return 1;
}
Step 3: Configure a task, compile with debug information.

In the VSC menu, click Terminal -> Configure default task -> Select compiler, I use C/C++ g++ build active. The task setting looks as

task.json:
        {
           "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        } 

It tells g++ how to compile your code (arguments you use in terminal), and where the compile output is. In the above, it is basic compile with debug infomation (-g).

${file} = test.cpp

${fileDirname} = fold path of test.cpp

${fileBasenameNoExtension}: test

So after compile success, the output is in current folder, naming test.

In VSC setting, there are many variables, i.e., variable in ${variable_name}. You can refer to https://code.visualstudio.com/docs/editor/variables-reference to learn what it means.

Step 4: Run and debug your code

In VSC menu, Run -> Start debugging. High probability chances, you will get Launch.json not found. Don’t worry. Follow instruction, and add it. The launch.json tell runner where your program is.

launch.json: 

{
  "configurations": [
  {
    "name": "(gdb) Launch",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceFolder}/${fileBasenameNoExtension}",
    "args": ["12"],
    "stopAtEntry": false,
    "cwd": "${fileDirname}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "setupCommands": [
        {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        },
        {
            "description":  "Set Disassembly Flavor to Intel",
            "text": "-gdb-set disassembly-flavor intel",
            "ignoreFailures": true
        }
    ]
  },
] 
Step 5: Add arguments for your program

Above test.cpp should be successfully run and debug in VSC. But sometime, we want to pass arguments into program. For example, in above test code, input integer value is from cin reading. You can also pass it as an argument. Here is modified code

test.cpp: 1 argument need. Remember program is as 1 argument. 

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int narg, char** args) {
    int i;

    cout << narg << "\n";
    if(narg != 2) {
        cout << "Warning: 1 argument needed\n";
    }
    cout << args[1] << "\n";
    i = atoi(args[1]);
    cout << "**** \n" << i << "\n";

    return 1;
}

You only need set args in launch.json, like

"args": ["12"],

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s