Linux/Manjaro配置Vscode的C/C++编译环境

安装gcc/g++
Arch/Manjaro安装gcc和g++非常简单
安装gcc和gcc-libs
sudo pacman -S gcc sudo pacman -S gcc-libs sudo pacman -S gdb
安装vscode
安装vscode,使用yay搜索软件包
yay -S visual-studio-code-bin
直接安装最新版本即可
如果还没有安装yay,需要通过git克隆yay的代码仓库
# 安装base-devel sudo pacman -S base-devel # 安装git sudo pacman -S git # cd到/opt cd /opt # 通过git克隆yay的代码仓库 sudo git clone https://aur.archlinux.org/yay.git #设置权限,将lin改为你的用户名 sudo chown -R lin:users ./yay # 进入yay文件夹并安装yay cd yay makepkg -si
然后再执行安装vscode的命令
yay -S visual-studio-code-bin
配置vscode的C/C++环境
安装插件
点击拓展,安装几个基本的插件:
- C/C++、C/C++ Extension pack、C/C++ Themes
- Code Runner:
- snippet:代码段模板
- Chinese:中文插件,安装好后重启vscode
先写一个hellow程序
在资源管理器中打开工作空间,我这里是/home/lin/Code/Cgit。 新建一个hellow文件夹,在文件夹中新建三个文件hellow.cpp map.cpp map.h用来方便测试后面的json文件是否配置完成。
编写下面的文件
//hellow.cpp
#include "map.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int i = 1;
int main()
{
cout << "Hellow World"
<< " " << AandB(i);
cout << endl;
}//map.cpp
#include <iostream>
using namespace std;
int AandB(int a)
{
a++;
return a;
}//map.h #ifndef MAP_H #define MAP_H extern int i; int AandB(int a); #endif
编写好后保存文件,下面就可以开始编写json文件,来引导vscode编译程序了。
编写json文件
json文件现在非常好读,通过chatgpt即可根据需求编写对应的json文件,这里将介绍一些比较基本的设置项和如何设置json文件以引导编译C/C++文件。
编写完的json文件可以在这个网站查询是否有效
编写vscode设置项json文件
首先点击文件-首选项-设置,右上角有一个打开设置的图标

这里会打开vscode设置项的settings.json文件,这个文件包含了一些vscode的基本设置。
{
"C_Cpp.files.exclude": {
"**/.vscode": true,
"**/.vs": true
},
"C_Cpp.default.customConfigurationVariables": {},
"code-runner.runInTerminal": true,
"code-runner.saveFileBeforeRun": true,
"C_Cpp.default.compilerPath": "/usr/bin/clang",
"files.autoGuessEncoding": true,
"security.workspace.trust.untrustedFiles": "open"
}上面是我的settings.json文件,大家可以直接用,其中code-runner需要安装code runner插件。
"C_Cpp.files.exclude": {...}:这个设置项是 C/C++ 扩展的配置,用于指定要排除在项目外的文件。在这个例子中,**/.vscode和**/.vs目录下的文件将被排除在项目外。"C_Cpp.default.customConfigurationVariables": {}:这个设置项是 C/C++ 扩展的配置,用于指定自定义的编译选项。在这个例子中,没有指定任何自定义编译选项。"code-runner.runInTerminal": true:这个设置项是 Code Runner 扩展的配置,用于指定代码是否在终端中运行。在这个例子中,设置为true,即代码将在终端中运行。"code-runner.saveFileBeforeRun": true:这个设置项是 Code Runner 的配置,用于指定代码是否在运行前保存。在这个例子中,设置为true,即代码将在运行前自动保存。"C_Cpp.default.compilerPath": "/usr/bin/clang":这个设置项是 C/C++ 扩展的配置,用于指定默认的编译器路径。在这个例子中,指定为/usr/bin/clang,即使用 clang 编译器。"files.autoGuessEncoding": true:这个设置项用于指定是否自动猜测文件编码。在这个例子中,设置为true,即 Visual Studio Code 将尝试自动猜测文件编码。"security.workspace.trust.untrustedFiles": "open":这个设置项用于指定未受信任的文件的打开方式。在这个例子中,设置为open,即未受信任的文件将以只读模式打开。
编写编译json文件
上面在工作空间中我打开的是/home/lin/Code/Cgit,并在这个文件夹下创建了一个hellow文件。在工作空间目录下创建一个新的文件夹.vscode,用来存放我们的json文件。 在新创建的.vscode文件中新建四个文件: c_cpp_properties.json :用来设置编译器的路径和头文件路径等信息。 launch.json:调试配置文件,用于在编写 C/C++ 代码时配置调试器 settings.json:设置项配置文件,用来设置一些编译C/C++程序时的一些设置选项 tasks.json:任务配置文件,用于定义一些自定义任务,以便在编辑器中执行它们。
创建后的工作空间是这样的

settings.json
{
"files.defaultLanguage": "cpp",
"editor.formatOnType": true,
"editor.formatOnSave": true,
"editor.snippetSuggestions": "top",
"code-runner.runInTerminal": true,
"code-runner.executorMap": {
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt.out",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt.out"
},
"code-runner.saveFileBeforeRun": true,
"code-runner.clearPreviousOutput": false,
"C_Cpp.clang_format_sortIncludes": true,
"C_Cpp.intelliSenseEngine": "default",
"cmake.configureOnOpen": false,
"files.associations": {
"iosfwd": "cpp"
}
}settings.json文件没有什么好改的,如果想自己设置可以问chatgpt
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/${workspaceFolderBasename}",
"/usr/include",
"/lib64/gcc/x86_64-pc-linux-gnu/12.2.1/include/"
],
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=7",
"__cdecl=__attribute__((__cdecl__))"
],
"browse": {
"path": [
"${workspaceFolder}",
"/usr/include",
"/lib64/gcc/x86_64-pc-linux-gnu/12.2.1/include/",
"/lib64/gcc/x86_64-pc-linux-gnu/12.2.1/include-fixed/",
"/lib64/gcc/x86_64-pc-linux-gnu/12.2.1/install-tools/include/"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}c_cpp_properties.json主要需要修改的是includePath,这个选项包含了头文件的地址,需要自己设置一下自己编写的头文件的目录,我比较习惯直接将头文件放在工程目录,就添加"${workspaceFolder}/${workspaceFolderBasename}" ${workspaceFolder}是工作空间名称变量代表 ${workspaceFolderBasename}是运行程序所在目录,如果我运行/home/lin/Code/Cgit/hellow/hellow.cpp,这个变量就是hellow 我添加的头文件目录就是/home/lin/Code/Cgit/hellow/,我们只需要在项目文件夹中编写头文件即可
如果习惯在项目文件夹中单独建一个问价夹存放头文件可以在
includePath添加"${workspaceFolder}/${workspaceFolderBasename}/include"把头文件放在include文件夹中即可
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "创建并调试C文件",
"type": "shell",
"command": "gcc",
"args": [
"-I",
"${workspaceFolder}/${relativeFileDirname}",
"-g",
"${workspaceFolder}/${relativeFileDirname}/*.cpp",
"-o",
"${workspaceFolder}/${relativeFileDirname}/${fileBasenameNoExtension}.out"
],
"group": "build"
},
{
"label": "创建并调试C++文件",
"type": "shell",
"command": "g++",
"args": [
"-I",
"${workspaceFolder}/${relativeFileDirname}",
"-g",
"${workspaceFolder}/${relativeFileDirname}/*.cpp",
"-o",
"${workspaceFolder}/${relativeFileDirname}/${fileBasenameNoExtension}.out"
],
"group": "build"
},
{
"label": "创建并运行C文件",
"type": "shell",
"command": "gcc",
"args": [
"-I",
"${workspaceFolder}/${relativeFileDirname}",
"${workspaceFolder}/${relativeFileDirname}/*.cpp",
"-o",
"${workspaceFolder}/${relativeFileDirname}/${fileBasenameNoExtension}.out"
],
"group": "build"
},
{
"label": "创建并运行C++文件",
"type": "shell",
"command": "g++",
"args": [
"-I",
"${workspaceFolder}/${relativeFileDirname}",
"${workspaceFolder}/${relativeFileDirname}/*.cpp",
"-o",
"${workspaceFolder}/${relativeFileDirname}/${fileBasenameNoExtension}.out"
],
"group": "build"
}
]
}tasks.json文件会生成.exe或.out文件,linux我们就选择.out文件,这里需要修改的是"args"这里对应的是gcc或g++编译指令。 我们这里设置的是
"args": [
"-I",
"${workspaceFolder}/${relativeFileDirname}",
"${workspaceFolder}/${relativeFileDirname}/*.cpp",
"-o",
"${workspaceFolder}/${relativeFileDirname}/${fileBasenameNoExtension}.out"
]在编译hellow.cpp的时候就会执行g++ -I /home/lin/Code/Cgit/hellow /home/lin/Code/Cgit/hellow/*.cpp -o /home/lin/Code/Cgit/hellow/hellow.out
下面是每个参数的解释:
g++:这个参数指定了使用的编译器,这个例子中为 g++ 编译器。-I /home/lin/Code/Cgit/hellow:这个参数指定了头文件的搜索路径,/home/lin/Code/Cgit/hellow是头文件所在的目录。/home/lin/Code/Cgit/hellow/*.cpp:这个参数指定了要编译的源文件,/home/lin/Code/Cgit/hellow是源文件所在的目录,*.cpp表示编译该目录下所有的 C++ 源文件。-o /home/lin/Code/Cgit/hellow/hellow.out:这个参数指定了生成的可执行文件的输出路径,/home/lin/Code/Cgit/hellow/hellow.out是生成的可执行文件的完整路径。
每次编译完成都会在项目目录中生成.out文件,当然也可以自己设置.out文件的保存路径,但同样需要修改launch.json文件,
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Run C",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${relativeFileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/${relativeFileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "创建并运行C文件"
},
{
"name": "Run C++",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${relativeFileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/${relativeFileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "创建并运行C++文件"
},
{
"name": "Debug C",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${relativeFileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/${relativeFileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "创建并调试C文件"
},
{
"name": "Debug C++",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${relativeFileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/${relativeFileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "创建并调试C++文件"
}
]
}launch.json文件设置了编译 C/C++ 代码时配置调试器,有几点需要特别注意
- "preLaunchTask"名称一定要与
tasks.json文件中的lable一致,这将影响调试时是否能找到对应的调试器 - "cwd"用于指定调试器的当前工作目录,要设置成你的项目目录,不然读取C++读取文件的时候相对路径会出现问题
- "program"要与
tasks.json文件中的args中的“-o”的地址一致,不然读取不到.out文件,我这里都设置的是`"workspaceFolder/{workspaceFolder}/workspaceFolder/{relativeFileDirname}/${fileBasenameNoExtension}.out"``
以上设置完成json文件,我们就可以运行C++程序了
运行hellow.cpp
- 资源管理器中点击hellow.cpp进入文件,点击左侧栏中带有小虫子图标的“运行和调试”
- 选择”debug C++“,这里的名称是在
launch.json中的"name"设置的找到对应的就行。 - 点击绿色三角运行。

- 终端输出
Hellow World 2表示配置成功

到此这篇关于Linux/Manjaro配置Vscode的C/C++编译环境的文章就介绍到这了,更多相关Vscode的C/C++编译环境内容请搜索好代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好代码网!




