Visual Studio Code & C programming on Linux

Tarang Patel
4 min readFeb 12, 2019

--

I have been playing with Visual Studio Code(VSCode) to develop C code on Linux. Lately, I have became a fan of Visual Studio Code which I believe is a great free product from Microsoft for developers in a long time.

I will show how to setup a Visual Studio Code on Linux CentOS and setup debugging C programming so that developers can step through the breakpoints in code. Please keep in mind that support for C/C++ on Visual Studio Code(VSCode) is still in preview so some things might not work as expected. The VSCode team is working hard to weed out some of the issues and release new features.

Disclaimer: I am not part of the Visual Studio Code team at the time of this writing.

Installing VSCode on CentOS

Microsoft has fallen in love with Linux since May 2015. That strategy helps them attract and become more developer friendly organization. That’s why the VSCode is available across Linux, macOS and Windows environments.

The details and most up-to-date steps are described here https://code.visualstudio.com/docs/setup/linux. I will show you a quick start guide for CentOS.

Open terminal instance and run following two commands:

sudo rpm --import https://packages.microsoft.com/keys/microsoft.ascsudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'

Update the package and install the VSCode:

yum check-update
sudo yum install code

Installing VSCode was easy…

Installing C/C++ Extensions

VSCode needs special extension to provide rich code editing options for C. The extension is developed by Microsoft. Click on Extensions icon on the sidebar and search for “c”. The first search result, “c/c++” by Microsoft is the popular extension I used. Install it.

Creating a work-space on VSCode and Hello World code

First thing after installing VSCode is to create a directory and add a code file (first.c) in the directory. Next, in VSCode, File ->Add Folder to Workspace…Select the directory you just created so that VSCode adds necessary files and dir(.vscode)to support compiling and debugging tasks later in the article. See my work-space below.

c_project work-space in my project

VSCode adds c_cpp_properties.json file to use rich editing option for C. My version of that file:

{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}

Compiling the code using tasks.json :

In order to compile the code, you need to add tasks.json file to the project. Type ctrl + shift + p to open the Command box and then select,

>Tasks: Configure Task command

>click Create tasks.json file from templates

> select Others.

Building the code using tasks.json :

Add a build group, Type ctrl + shift + b to open the Command box. This will add the build group. Select the No build tasks… and select build first.c. In your case, it will be your label value from tasks.json

Final version of tasks.json is shown below:

Note that my label value is “build first.c”. You can put any text, we will use the same label value in launch.json :

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build first.c",
"type": "shell",
"command": "gcc -Wall -g first.c -o first",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

Important Note: In the above, tasks.json command value is gcc. That means I am using gcc compiler to compile the code. Make sure that you have gcc compiler installed. In the terminal, run gcc --version to check gcc version. Also, note that I am using -Wall to display details in the build and -g option to tell OS to generate extra info for debugging. See here for more details: https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#Debugging-Options

At this point you should be able to build your code.

Debugging the code using launch.json :

You will need launch.json to use debugging. Go to Debug view in the Sidebar and click gear icon to open the launch.json file. I edited preLaunchTasks tag in launch.json file to match the label value from the tasks.jsonfile. I also edited the program tag to provide output file name. Here is my final version of launch.json.

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit:https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/first",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "build first.c",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

That’s it…

You can now put a break point in your C code and hit f5 key to launch the app in debugger and it will hit the first break point in main().

You can see the variables in the debug console.

Enjoy coding and fun debugging…:)

--

--