This paper reference from VS Code official documentation, a simplified and complement to some if you want to get more details, please read the official document code.visualstudio.com/docs/cpp/co…
Linux is closely associated with THE C language, and writing C code in Linux is even more enjoyable. If you want to do a good job, you must sharpen your tools first. A comfortable development environment is very important. Linux has the compiler GCC, the debugger GDB, and the editor vim. But vim was difficult to use, so we chose VS Code, which was more user-friendly, and built a complete programming environment with extensions.
VS Code installed
Red Hat and Debian
Go to VS Code’s website to download the package: code.visualStudio.com
If your Linux distribution is Rea Hat, such as Fedora, CentOS, or RHEL, download the.rpm file. If your Linux distribution is in the Debian family, such as Debian, Ubuntu, Mint, ElementaryOS, download the.deb file.
After the software package is downloaded, open the terminal and go to the directory where the software package is stored to install it.
For Red Hat systems, use yum:
Sudo DNF install./ PackageCopy the code
For the Debian family, use apt:
/ sudo apt installCopy the code
Arch Linux system
This section applies to Arch Linux as well as distributions based on Arch Linux such as Manjaro.
PKG packages for ArchLinux are not available on the VS Code website, but the ArchLinux community has a powerful AUR(ArchLinux User-community Repository) to find out how to build VS Code packages.
Although there is an open source version of VS Code ‘Code’ in the Arch Linux repository, I chose the official binary because of the ugly icon
AUR website: aur.archlinux.org
Enter ‘Visual Studio Code’ in the search box on the right, and click ‘Visual -studio-code-bin’ in the search result to go to the package page.
Click ‘Download Snapshot ‘in the small window in the upper right corner.
The downloaded file is a compressed package containing a folder, unzip it, as shown in the figure below:
Open the terminal and go to the extracted folder, in this case ‘visual-Studio-code-bin’, and enter the following command to start building the package.
makepkg -si
Copy the code
This command will download the required resources based on the contents of the ‘PKGBUILD’ file in the folder and build the package as Arch Linux, install the dependencies using the package manager, and install the package when the build is complete.
If you are careful enough, you will notice that there is an extra file with the suffix ‘pkg.tar.zst’ in the post-build folder. This is the Arch Linux package. Save it and you can install VS Code with Pacman without a network.
Other distributions
From the VS Code website, select ‘Other platforms’ and download the file with the suffix ‘tat.gz’, which is a zip package containing a folder. Unzip it, go to the folder, and run a program named ‘code’ to start VS Code.
C Programming environment configuration
Install compile and debug software
The optional compilation and debugging software collection includes gcc-gDB and clang-llDB. Generally, GCC and GDB are used under Linux, and CLang and LLDB are used under MacOS.
The following commands represent commands to install GCC and GDB on Rad Hat, Debian, and Arch Linux distributions, respectively.
sudo dnf install gcc gdb
Copy the code
sudo apt install gcc gdb
Copy the code
sudo pacman -S gcc gdb
Copy the code
Install the VS Code extension
Search for ‘c’ in the extension and install the first Microsoft ‘C/C++ ‘.
VS Code’s C extensions are not as good as VS’s, and the Code hints, completion, auto indentation, etc., are not good enough, so the score is low.
You can also install the ‘Chinese’ Chinese extension if you wish.
Configuring the programming environment
Configuration File Introduction
VS Code uses folders as a workspace, so you should first create a folder to hold your project, and then open that folder with VS Code.
The configuration files of the project are stored in the folder ‘.vscode’. The configuration files are in JSON format. The main configuration files are as follows:
Json: ‘C/C++’ extension Settings for the current workspace. Json: VS Code Settings for the current workspace
Not all of the above configuration files are required and can be created as required. Also, VS Code automatically creates configuration files when we need specific features, so you don’t need to manually create the ‘.vscode’ folder and configuration files, just leave the entire workspace empty at this stage.
Configuring the Compilation Environment
Create a new source file in the current workspace, such as ‘main.c’, and write something else, such as:
#include <stdio.h>
int main(void) {
printf("hello, world\n");
return 0;
}
Copy the code
Then choose Terminal > Configure Default Build Task on the top menu bar, and select C/C++: GCC Build active File in the pop-up box.
At this point VS Code creates the ‘.vscode’ folder and creates the ‘tasks.json’ configuration file under it.
Open the ‘tasks.json’ file, which looks like this:
{
"version": "2.0.0"."tasks": [{"type": "shell"."label": "C/C++: gcc build active file"."command": "/usr/bin/gcc"."args": [
"-g"."${file}"."-o"."${fileDirname}/${fileBasenameNoExtension}"]."options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"]."group": {
"kind": "build"."isDefault": true}}}]Copy the code
Type: Optional ‘process’ or ‘shell’, determines whether the build task is started as a separate process or as a shell command. If it is ‘shell’, you can use Linux’s shell extension label: the name of the build task, which is also the task identifier. Command: The command used by the build task, which is the command that calls the compiler, such as’ GCC ‘args: the argument passed to command, which is the GCC option
-g
: Contains debugging information for GDB debugging${file}
: Indicates the file to be compiled-o
: Specifies the path and name of the executable file, which follows the ‘${fileDirname}/${fileBasenameNoExtension}’ argument
CWD: build command execution working directory, and relative location search related to file generation problemMatcher: use regular expressions to check source files for errors, configuration syntax is complicated, and generally not used. Delete this configuration if you don’t need it. IsDefault: whether to use it as the default build configuration
The default generated configuration file is ready to compile, and you only need to modify the values of certain parameters to achieve the desired effect.
Configuring the Debugging Environment
Open the previously created source file again, and choose Run > Add Configuration on the top menu bar. In the dialog box that is displayed, choose C++ (GDB/LLDB) > gcc-build and debug active file.
At this point VS Code creates the ‘launch.json’ configuration file in the ‘.vs Code ‘folder.
The debugging process is also started and completed. The debugging information is displayed on the ‘DEBUG CONSOlE’. If you do not use GDB to DEBUG manually, you do not need to care about the contents of the ‘DEBUG CONSOlE’.
The program output is displayed in ‘TERMINAL’, which is VS Code’s new shell. After the program is finished, you can execute the input command just like any other shell.
On the right side of the terminal, you can switch shells, such as those created by a build task.
This shell cannot be used to enter commands, but can be used repeatedly by build tasks. We need to pay a lot of attention to this shell because this is where GCC’s output during compilation, including warnings and errors from source files, is often more detailed and accurate than the output from VS Code’s ‘C/C++’ extension in the ‘PROBLEMS’ window.
The ‘launch.json’ configuration file looks like this:
{
// 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": "gcc - Build and debug active file"."type": "cppdbg"."request": "launch"."program": "${fileDirname}/${fileBasenameNoExtension}"."args": []."stopAtEntry": false."cwd": "${workspaceFolder}"."environment": []."externalConsole": false."MIMode": "gdb"."setupCommands": [{"description": "Enable pretty-printing for gdb"."text": "-enable-pretty-printing"."ignoreFailures": true}]."preLaunchTask": "C/C++: gcc build active file"."miDebuggerPath": "/usr/bin/gdb"}}]Copy the code
Name: indicates the name of a debugging task. If there are multiple debugging tasks, this parameter can be used to identify the debugging task type. You can only fill in ‘CPPDBG’ request: Optionally ‘launch’ or ‘attach’ determines whether the debugged program should be a standalone process or attached to another process. Program: target program, corresponding to the program output in ‘tasks.json’ args: Parameters passed to the target program stopAtEntry: CWD: the working directory of the target program, which is related to reading and writing files in relative locations. Environment: The environment variable passed to the target program externalConsole: Whether to use an external console, if ‘true’, a new window will pop up to run the target program and close MIMode: debugger immediately after the program ends, either ‘GDB’ or ‘LLDB’, in this case ‘GDB’ setupCommands: PreLaunchTask: preLaunchTask, identified by name, configured in ‘tasks.json’ as build task miDebuggerPath: Path to the debugger, if not set, then look in the environment variable for the MIMode program
Start debugging
VS Code debugs in a similar way to VS, you can add breakpoints to the left of the number of lines, or you can right-click the breakpoints area to add conditional breakpoints. Then press F5 to start debugging.
Structured debugging information is displayed in the debug window on the left.
Manage the project
The configuration file in ‘.vscode’ is valid for all files in the current working directory, and we use variables in ‘tasks.json’ and ‘launch.json’ to determine which file is currently being processed, rather than specifying a filename, so all source files can be built and debugged in the same way.
The working directory with multiple single files is roughly as shown below, and if you find the resulting executable unsightly, you can also modify the configuration files to put them all in ‘.vscode’.
This single-source compilation and debugging approach is ideal for practice or problem solving.
For software engineering, however, putting all the code in a single file is unacceptable, no matter how small the project. In a future article, I’ll show you a way to manage multi-file C projects in VS Code (spoiler, no cmake).
A little tip
- This article only describes the environment configuration under Linux, Windows users please explore
- This article covers only the configuration of a C programming environment; explore C++ for yourself
- GCC default link to standard C library ‘libc‘, but do not link to other libraries if you want to use the math library. ‘libm‘, except it needs to be in the source file
#include <math.h>
And you need to add ‘-lm’ to the compile options. - If you know some of the GCC parameters, you can get a lot of useful information in the ‘task’ shell