操作系统环境:  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  。

"cwd": "${fileDirname}"  ,  “cwd” 指定当前目录
 
 
 
 
 
 
 
===================================================== 

.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++编译环境的更多相关文章

  1. WIN7环境下配置vscode c++环境

    目录 安装vscode 添加中文环境支持 添加c++支持 配置c++环境 安装MinGW 配置MinGW环境变量 配置vscode launch文件配置 task文件配置 可能出现的问题 安装vsco ...

  2. Ubuntu16.04下配置VScode的C/C++开发环境

    博客转载:https://blog.csdn.net/weixin_43374723/article/details/84064644 Visual studio code是微软发布的一个运行于 Ma ...

  3. Win7平台下配置Sublime Text2 的C++编译环境

    Sublime Text 是一个跨平台的编辑器,之前在 Mac 上成功配置了 C++ 在 Sublime Text 的编译环境,接下来介绍下载 windows 平台下的环境配置. 1. 首先判断机器上 ...

  4. 在Ubuntu环境下配置Proxmark3(PM3)使用环境

    参考资料:PM3官方Wiki 因为国内网络上大多是在Kali系统上使用PM3的教程(链接1.链接2.链接3),而这些教程的步骤对于Ubuntu系统并不完全适用.所以写下本文,记录我个人的安装经历. 本 ...

  5. Win10环境下配置VScode的C++编译环境

    写前感想:前前后后,折腾好几次,最后还是在学长安利下,开始入坑vscode了.原因一个是小巧,还有就是vs新建工程码题的方式太消耗内存了,基本每个项目就是以MB为单位计算的,然后希望用这篇文章记录自己 ...

  6. 在Linux虚拟机下配置jdk的环境变量

    1.到Oracle公司的官网里下载好jdk,网址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...

  7. 阿里云ECS服务器Linux环境下配置php服务器(二)--phpMyAdmin篇

    上一篇讲了PHP服务器的基本配置,我们安装了apache,php,还有MySQL,最后还跑通了一个非常简单的php页面,有兴趣的朋友可以看我的这篇博客: 阿里云ECS服务器Linux环境下配置php服 ...

  8. Linux环境下使用VSCode编译makefile文件的注意事项

    Linux环境下使用VSCode编译makefile文件的注意事项 首先安装C/C++的两个依赖 在debug,launch会自动的生成下方的launch.json launch.json { // ...

  9. 【经验之谈】Windows环境下配置WordPress

    前言 wordpress全球著名的开放博客平台,拥有成千上万个各式插件和不计其数的主题模板样式,使用php和mysql搭建,下面说下载windows环境下配置wordpress,经验之谈. 安装 关于 ...

  10. Ubuntu环境下配置GCC

    Ubuntu网络环境下安装GCC及其头文件步骤: 1.Ubuntu环境下配置GCC 刚装好的GCC什么都不能编译,因为没有一些必须的头文件,所以要安装build-essential,安装了这个包会安装 ...

随机推荐

  1. Java映射 转换post response T data

    Java映射 转换post response data 接上篇Java泛型对象在http请求和响应对象中的封装https://www.cnblogs.com/oktokeep/p/17688322.h ...

  2. 09-CentOS软件包管理

    简介 CentOS7使用rpm和yum来管理软件包. CentOS 8附带YUM包管理器v4.0.4版本,该版本现在使用DNF (Dandified YUM)技术作为后端.DNF是新一代的YUM,新的 ...

  3. 初学者必读:如何使用 Nuxt 中间件简化网站开发

    title: 初学者必读:如何使用 Nuxt 中间件简化网站开发 date: 2024/6/24 updated: 2024/6/24 author: cmdragon excerpt: 本文概述了N ...

  4. STM32学习笔记:创建标准库工程模板

    背景 标准库下载:地址 本章的项目可以在这里下载. STM32作为一类经典的MCU.本人从2018年1月1日开始对于STM32系列单片机的学习. 本人所持的型号为:STM32F429ZI-DISCOV ...

  5. 生成数字csv

    csv文件: import time import mcpi.minecraft as minecraft import mcpi.block as block mc = minecraft.Mine ...

  6. 《DNK210使用指南 -CanMV版 V1.0》第五章 编译CanMV固件

    第五章 编译CanMV固件 1)实验平台:正点原子DNK210开发板 2) 章节摘自[正点原子]DNK210使用指南 - CanMV版 V1.0 3)购买链接:https://detail.tmall ...

  7. PHP 真的不行了?透过 PHP 的前世今生看真相

    大家好,我是码农先森. 1994年我出生在湖南的农村,就在同年加拿大的拉斯姆斯·勒多夫创造了 PHP,这时的 PHP 还只是用 Perl 编写的 CGI 脚本.或许是时间的巧合 PHP 变成了我后半生 ...

  8. SpringBoot集成Mongodb文档数据库

    添加Maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  9. Spring Cloud提供者actuator依赖

    <!-- actuator依赖 --> <dependency> <groupId>org.springframework.boot</groupId> ...

  10. idea 提交代码到GitHub

    配置账户 配置Git安装目录 一般默认识别,其他参数不变 配置GitHub账户 提交到GitHub 1.VCS->import into version control -> share ...