操作系統環境: 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