Setting up Visual Studio Code for Simple C/C++ development

On 2016/06/13 at 16:12

I tried Visual Studio Code the other day and I found it really good for simple coding jobs(ie. competing in hackerrank). Realtime symbol tagging and debugging can be done in vscode by installing cpptools plugins which makes vscode acting as a lightweight IDE for me. Here I will show how to set up the vscode for simple C/C++ development. The tutorial in the official blog is a bit incomplete in my opion.

We have to install cpptools and C++ Intellisense first. In order to use C++ Intellisense, we have also to install GNU Global tools. (In fedora, I can simply install GNU Global tools by dnf install global.)

Second thing is to make the code-editing aids feature work, it does not automatically generate the browse.VC.db file on my Fedora 23. I have to press F1 to brings up the command palette and type C/Cpp Edit configuration. It will open a c_cpp_properties.json file and simply press ctrl+s you can save it to the .vscode folder under your project folder(You may need to point the includePath to the correct path). After the generating the c_cpp_properties.json, the symbol will start to be tagged and the code-editing aids feature will work.

The next thing is to generate the task.json for automatic build after you press ctrl+shift+b. The first time you press ctrl+shift+b there will be a dialog prompt up asking you edit build configuration. Here I used the other option to manualy use external command to build my code. The second element in the args array means using the current file name as the argument. After setting up the task.json, we can build our code by simply pressing ctrl+shift+b.

{
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-g", "${file}"],
    "showOutput": "always"
}

The last thing is to make debugging feature work. First time pressing F5 will generate a launch.json under your .vscode folter. Just pointing the program attribute value to you ${workspaceRoot}/a.out then you are done(Because our task.json use default output binary name for g++).

After completing all the settings, you can now build the code by pressing ctrl+shift+b and run it by pressing F5. You can even set breakpoint and debug using the debug feature. The only on drawback is cpptools plugin can not show the content of STL containers even though my GDB supports showing content of STL container. I hope showing STL containers content feature can be added into cpptools soon :).

Comments