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_java_同时停止三个项目脚本
- qt实现web服务器加载vue应用进行C++和html混合编程-连载【6】-企业级系统开发实战连载系列 -技术栈(vue、element-ui、qt、c++、sqlite)
- Linux系统:centos7下搭建ZooKeeper3.4中间件,常用命令总结
- VSCode调试Html中的脚本 vscode前端常用插件推荐,搭建JQuery、Vue等开发环境 vsCode 添加浏览器调试和js调试的方法总结 VS Code - Debugger for Chrome调试js
- 托管C++线程锁实现 c++11线程池
- linux下c/c++实例之socket服务器实例
- Linux【IO】6. Redis响应严重延迟,如何解决?
- Linux&Tina&Melis内存布局分析以及linux reserved memory机制
- [转]六款常用的linux C/C++ IDE
- linux环境下C++代码打印函数堆栈调用情况
- Linux 环境下使用g++编译C++
- linux:C++的socket编程
- 《嵌入式Linux开发实用教程》——1.3 arm-linux交叉编译链
- 孙玄&辜教授:基于Linux内核的时间轮算法设计实现【附代码】
- 第13章 Linux的网络管理
- 《Linux嵌入式实时应用开发实战(原书第3版)》——3.8 获得帮助
- Linux_查看linux并发连接数
- Linux 如何将linux主机变为路由器&&iptables的基本用法
- 【C++】基于linux文件管理和C++的I/O框架,实现命令行下的文件管理器
- linux程序设计——运行SQL语句(第八章)
- 【Linux】linux经常使用基本命令
- Linux学习笔记(25)linux批量管理
- Linux学习笔记(3)linux服务管理与启停(开机自启与自定义服务)
- Linux C/C++方向开发(13周学习路线)
- Linux之用户管理
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---44
- Linux 之 rsync实现服务器的文件同步