Setting up vs-code for C/C++ on Linux

Setting up vs-code for C/C++ on Linux

In this short article we are going to setup our vs-code environment for c or c++ programming and debugging on Linux.

vs-code extentions

to start coding c or c++ in vscode you need to install some extensions which help you visually and some speed your code with useful code snippets.

the recommended list of extensions are:

  • C/C++ by Microsoft
  • C/C++ Extension Pack by Microsoft
  • bracket pair colorizer 2 by CoenraadS
  • Cmake Tools by Microsoft

and language parsing extensions for:

  • c/c++
  • Make
  • CMake

Choosing compiling method

first thing we need to do is to choose our compiler and the way we are to compile our source code files.

for example we could use gcc or clang for compiler and we could build our source files using make, cmake or directly compiling with the commands.

as of compiler, both clang and gcc (or g++) are more than enough and really don't differ that much, but here I'm using gcc but almost everything is the same with clang in a basic level.

for the build method I use make and its a personal preference so feel free to choose whatever suits you better.

if you don't know what makefiles are check out this link:

vs-code Tasks

after writing up your build script, time to build your tasks in vs-code.

for this open up vs-code command pallet by pressing Ctrl+Shift+P or from the View menu. in there type "tasks" and this option must show up:

No alt text provided for this image

select the "Tasks: Configure Task" option.

No alt text provided for this image

Select "Create task.json file from template"

No alt text provided for this image

and at last select "Others" option.

now you must be left an empty file name tasks.json in the .vscode folder. as you can see in the tasks section we have one default task named "echo". we are going to make this task our compiling task, so first, change the label to whatever name you like but preferably some name related to the process. here I'm gonna name mine "Build Debug". for the type lets leave it out as "shell" for now. and for the command depending on what build process you choose to have, put the command that compiles your source files. as I'm using make, my command will be "make". if your command has arguments after it, after "command" tag, add another tag like bellow and add your arguments one after another as a list of strings:

"command" : "make",
"args": [
 "clean",
 "all" // <------ last item in a json link must not have "," after it
],
        

after that go ahead and add the following options too:

"options": {
    "cwd": "${workspaceFolder}"            // 1
},
"group": {
    "kind": "build",                       // 2
    "isDefault": true
},        

what these 2 options does is:

  1. "cwd" specifies this task's shell working directory. if you don't point it to the root directory of your project where the makefile is located, when this task is summoned, it will complain about not finding the makefile.
  2. the "group" option is not a must but it helps make things simpler when we go further. and its probably obvious what its options do.

by now your tasks.json should look something like:

No alt text provided for this image

vs-code launch configuration

now that we have our build task, its time for the final step which is: configure launch.json. go to the debug tab in vscode and just bellow the "Run and Debug" button, there is an option "create a lunch.json file" marked blue to emphasis it's a link. go ahead and click on that. in the menu click on one of the items preferably the c++ gdb option if it is there or, just go in the .vscode folder, where the tasks.json is located and create a file named launch.json.

now inside the launch.json file in the tag named "configurations", add the following configuration:

{            
    "name": "Debug",
    "type": "cppdbg",
    "request": "launch",                                // 1
    "program": "${workspaceFolder}/Build/CoreKit.elf",  // 2
    "cwd": "${workspaceFolder}",
    "externalConsole": false,                           // 3
    "preLaunchTask": "Build Debug",                     // 4
    "miDebuggerPath": "/usr/bin/gdb"                    // 5
}        

here's what these tags do:

  1. request type is telling vs-code that we want to run an application
  2. specifies the program we want to run
  3. if true, for application launch another separate terminal is opened
  4. marks the build task before it actually tries to run the app
  5. points vs-code to your gdb debugger

your launch.json should look something like this at this point:

No alt text provided for this image

at this point your basic setup is done. although there is a lot more that we can do such as compiling the editing source file or more, this is a very basic scheme that is easy and fast to setup and works for a wide range of c/c++ projects.

i hope yo enjoyed.

here's more if you'd like to get deeper:


要查看或添加评论,请登录

kian khajeh arzani的更多文章

  • A modern survival guide

    A modern survival guide

    We are living in an interesting time for sure. We have developed many tools and have improved our quality of life…

    4 条评论
  • Runtime Loose Inheritance Pt.2

    Runtime Loose Inheritance Pt.2

    Now that we know the properties of our container, we can attempt to implement it. First things first lets create a C++…

  • Runtime Loose Inheritance

    Runtime Loose Inheritance

    My first goal is to help you understand the problem which gave rise to this solution. From there we derive the…

  • GNU Make

    GNU Make

    What is GNU Make? GNU make or widely known as Makefile, is a scripting language designed for automating the build…

社区洞察

其他会员也浏览了