Linux环境下配置vscode的C/C++编译环境
操作系统环境: Linux
配置vscode的C/C++编译环境需要安装插件:


本文的配置是指在linux下不使用vscode插件中自动配置,而是采用手动编写配置文件。主要原因是插件自动生成的C/C++配置文件功能不全面,为了更好的适应C/C++的语言特性、编写功能更强大的C/C++语言,所以采用手动编写配置文件。
========================================================
VSCODE中C/C++配置需要最少两个文件:
.vscode/task.json
.vscode/launch.json
本文中demo的C语言代码:
mainX.c
#include<stdio.h> void main()
{
int a=0;
a++;
a+=2;
a-=3;
printf("a=%d\n",a);
return;
}

运行结果:

===========================================================
.vscode/task.json 为C/C++项目配置编译条件:
{
"tasks": [
{
"type": "shell",
"label": "C/C++: gcc-7 生成活动文件",
"command": "/usr/bin/gcc-7",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}111"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
"command": "/usr/bin/gcc-7", 指定c/c++编译器路径
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}111"
],
“arg” 参数中“-g” 表示编译生成的可执行文件带有调试信息,我们一般习惯在该参数后指定需要编译的源文件,其中${file}指的是当前打开的当前文件,这里我们也可以改写该文件名,不然的话每次编译都要保证当前打开的文件是需要编译的文件,这里指的需要编译的文件是指 main 函数所在的文件。
“-o” 是指编译后的文件存储地址和文件名,${fileDirname}指的是当前打开文件所在的目录, ${fileBasenameNoExtension}指的是当前打开文件的不带扩展名后的文件名,这里我们为了区别名称使用 ${fileBasenameNoExtension}111 意味着编译后的文件名为 mainX111 。

.vscode/launch.json 为C/C++项目配置运行条件:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc-7 - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}111",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc-7 生成活动文件",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
"program": "${fileDirname}/${fileBasenameNoExtension}111",
"program"指定需要执行的文件路径
"preLaunchTask": "C/C++: gcc-7 生成活动文件",
"preLaunchTask" 指定运行编译好文件前需要执行的任务
需要注意的是 "preLaunchTask" 中的值 "C/C++: gcc-7 生成活动文件" 需要和 task.json 中的"label" 值 "C/C++: gcc-7 生成活动文件"保持一致,否则的话运行编译好的文件时会报错,因为vscode会由于找不到需要执行编译的配置信息而没有进行编译从而导致报错。
========================================================
.vscode/task.json
{
"tasks": [
{
"type": "shell",
"label": "build task",
"command": "/usr/bin/gcc-7",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
.vscode/launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc-7 - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build task",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
=============================================================
参考资料:
https://code.visualstudio.com/docs/editor/variables-reference

Predefined variables
The following predefined variables are supported:
- ${workspaceFolder} - the path of the folder opened in VS Code
- ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
- ${file} - the current opened file
- ${fileWorkspaceFolder} - the current opened file's workspace folder
- ${relativeFile} - the current opened file relative to
workspaceFolder - ${relativeFileDirname} - the current opened file's dirname relative to
workspaceFolder - ${fileBasename} - the current opened file's basename
- ${fileBasenameNoExtension} - the current opened file's basename with no file extension
- ${fileDirname} - the current opened file's dirname
- ${fileExtname} - the current opened file's extension
- ${cwd} - the task runner's current working directory on startup
- ${lineNumber} - the current selected line number in the active file
- ${selectedText} - the current selected text in the active file
- ${execPath} - the path to the running VS Code executable
- ${defaultBuildTask} - the name of the default build task
- ${pathSeparator} - the character used by the operating system to separate components in file paths
Linux环境下配置vscode的C/C++编译环境的更多相关文章
- WIN7环境下配置vscode c++环境
目录 安装vscode 添加中文环境支持 添加c++支持 配置c++环境 安装MinGW 配置MinGW环境变量 配置vscode launch文件配置 task文件配置 可能出现的问题 安装vsco ...
- Ubuntu16.04下配置VScode的C/C++开发环境
博客转载:https://blog.csdn.net/weixin_43374723/article/details/84064644 Visual studio code是微软发布的一个运行于 Ma ...
- Win7平台下配置Sublime Text2 的C++编译环境
Sublime Text 是一个跨平台的编辑器,之前在 Mac 上成功配置了 C++ 在 Sublime Text 的编译环境,接下来介绍下载 windows 平台下的环境配置. 1. 首先判断机器上 ...
- 在Ubuntu环境下配置Proxmark3(PM3)使用环境
参考资料:PM3官方Wiki 因为国内网络上大多是在Kali系统上使用PM3的教程(链接1.链接2.链接3),而这些教程的步骤对于Ubuntu系统并不完全适用.所以写下本文,记录我个人的安装经历. 本 ...
- Win10环境下配置VScode的C++编译环境
写前感想:前前后后,折腾好几次,最后还是在学长安利下,开始入坑vscode了.原因一个是小巧,还有就是vs新建工程码题的方式太消耗内存了,基本每个项目就是以MB为单位计算的,然后希望用这篇文章记录自己 ...
- 在Linux虚拟机下配置jdk的环境变量
1.到Oracle公司的官网里下载好jdk,网址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...
- 阿里云ECS服务器Linux环境下配置php服务器(二)--phpMyAdmin篇
上一篇讲了PHP服务器的基本配置,我们安装了apache,php,还有MySQL,最后还跑通了一个非常简单的php页面,有兴趣的朋友可以看我的这篇博客: 阿里云ECS服务器Linux环境下配置php服 ...
- Linux环境下使用VSCode编译makefile文件的注意事项
Linux环境下使用VSCode编译makefile文件的注意事项 首先安装C/C++的两个依赖 在debug,launch会自动的生成下方的launch.json launch.json { // ...
- 【经验之谈】Windows环境下配置WordPress
前言 wordpress全球著名的开放博客平台,拥有成千上万个各式插件和不计其数的主题模板样式,使用php和mysql搭建,下面说下载windows环境下配置wordpress,经验之谈. 安装 关于 ...
- Ubuntu环境下配置GCC
Ubuntu网络环境下安装GCC及其头文件步骤: 1.Ubuntu环境下配置GCC 刚装好的GCC什么都不能编译,因为没有一些必须的头文件,所以要安装build-essential,安装了这个包会安装 ...
随机推荐
- Kubernetes OOM 和 CPU Throttling 问题
介绍 使用 Kubernetes 时,内存不足(OOM)错误和 CPU 限制(Throttling)是云应用程序中资源处理的主要难题.为什么呢? 云应用程序中的 CPU 和内存要求变得越来越重要,因为 ...
- const 和 volatile 指针
关键字 const 和 volatile 规定了指针的处理方式: const 规定指针在初始化后是受保护的,不能够再修改. volatile 规定了变量的值能够被用户应用程序外部的操作所修改. 因此, ...
- SpringBoot指标监控功能
SpringBoot指标监控功能 随时查看SpringBoot运行状态,将状态以josn格式返回 添加Actuator功能 Spring Boot Actuator可以帮助程序员监控和管理Spring ...
- UE 5 NavMesh 烘培 逻辑流程
关于UE引擎层面的东西: 在向场景重拖入一个NavMeshBoundsVolume时(或者修改时). 会调用 void UNavigationSystemV1::PerformNavigation ...
- Numpy技巧: 由label获得相等矩阵
Numpy技巧: 由label获得相等矩阵 假设Label为: [ABAC] , 如何方便的得到一个矩阵, 其元素i,j表示第i位和第j位相等呢? 先把Label复制扩展成: m,m 的 ...
- Android系统启动:.rc文件
Android系统启动:.rc文件 reference : https://www.jianshu.com/p/a4c17f0110d0 以init.rc为例. .rc文件 init.rc文件由系统第 ...
- 3568F-Linux系统启动卡制作及系统固化
- vulnhub - w1r3s.v1.0.1
vulnhub - w1r3s.v1.0.1 高质量视频教程 - b站红队笔记 靶机下载 本地环境 本机ip:192.168.157.131 w1r3s虚拟机设置NAT模式 信息收集 扫描网段得到攻击 ...
- SpringBoot异步任务EnableAsync
什么是一部任务和使用场景:适用于处理log.发送邮件.短信...等 下单接口->查库存 1000 余额校验 1500 风控用户 1000 启动类里面使用@EnableAsync注解开启功能,自动 ...
- win10打不出中文的修复方法!
说明 在Win10系统中,默认自带了中文输入法,使用起来非常的方便,但有时win10系统中自带的输入法会打不出中文的情况,该怎么办呢?遇到这样的问题,我们可以参考下本文中的方法来修复. 步骤: cmd ...