C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > Vscode配置C/C++编译环境

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

作者:Morgana

这篇文章主要介绍了Linux/Manjaro配置Vscode的C/C++编译环境,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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++环境

安装插件

点击拓展,安装几个基本的插件:

先写一个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文件可以在这个网站查询是否有效

Fetching Title#u3bx

编写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插件。

编写编译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

下面是每个参数的解释:

每次编译完成都会在项目目录中生成.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++ 代码时配置调试器,有几点需要特别注意

以上设置完成json文件,我们就可以运行C++程序了

运行hellow.cpp

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

您可能感兴趣的文章:
阅读全文