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:
and language parsing extensions for:
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:
select the "Tasks: Configure Task" option.
Select "Create task.json file from template"
领英推荐
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:
by now your tasks.json should look something like:
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:
your launch.json should look something like this at this point:
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: