操作系统环境:  Linux


配置vscode的C/C++编译环境需要安装插件:

Linux环境下配置vscode的C/C++编译环境_json


Linux环境下配置vscode的C/C++编译环境_json_02

本文的配置是指在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;
}


Linux环境下配置vscode的C/C++编译环境_C/C++_03

运行结果:

Linux环境下配置vscode的C/C++编译环境_活动文件_04


===========================================================

  .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  。



Linux环境下配置vscode的C/C++编译环境_活动文件_05


"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/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​

Linux环境下配置vscode的C/C++编译环境_C/C++_06




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