-
需要安裝的擴展 C/C++
如果是遠程 Linux上開發還需要安裝 Remote Development
-
創建工作目錄后,代碼遠程克隆... 省略..
-
創建項目配置文件,主要的作用是代碼智能提示,錯誤分析等等...
按F1,輸入 C/C++ 選擇 編輯配置UI或者json 這個操作會生成 .vscode/c_cpp_properties.json 配置文件
修改相關的參數,如頭文件路徑,預定義參數,編譯器等
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**" // 包含了當前工作下所有頭文件,如果沒有在該目錄下需要引入
],
"defines": [ // 這里是項目中需要的預定義 LINUX環境通常需要加LINUX
"LINUX",
"ACI_TEST"
],
"compilerPath": "/usr/bin/g++", // 編譯器程序
"cStandard": "gnu99",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
(注意) 這個配置文件與編譯和執行沒有任何關系,它僅僅用於代碼提示和錯誤提示
- 代碼編譯和調試配置
選中打開一個.c 或者 .cpp 文件,按F5 選擇 環境 和 編譯器
生成了2個文件
.vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}", // 需要調試的程序路徑,如 ${workspaceFolder}/test
"args": [], // 調試程序需要接收的參數,如 test -c => "args": ["-c"]
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file", // 在調試之前需要做什么, 就是當你按下f5, 首先會去不如編譯程序... make 或者 gcc ,g++ 編譯上面路徑的程序,
// "C/C++: g++ build active file" 只是一個名稱,它會去 tasks.json 中找這個名稱, 執行 這個任務之后開始調試
"miDebuggerPath": "/usr/bin/gdb", // 調試的gdb ,要確保環境已經安裝了gdb, sudo apt install gdb
"envFile": "${workspaceFolder}/.env" // 配置環境變量的文件路徑,下面會繼續說明用處.
}
]
}
.vscode/tasks.json
{
"tasks": [
{
"type": "cppbuild", // shell 類型...
"label": "C/C++: g++ build active file", // preLaunchTask 的名稱
"command": "/usr/bin/g++", // 編譯器
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
這個文件就是怎么去編譯你的程序,沒什么可說 , make 或者 gcc g++ 編譯
貼一個我的項目 tasks 用make 編譯
{
"tasks": [
{
"type": "shell",
"label": "make_1",
"command":"make -j6 -f makefile_local.mk 2>build_error.log",
"options": {
"cwd": "${workspaceFolder}/UnitTesting/"
},
"problemMatcher": []
},
{
"type": "shell",
"label": "make_2",
"command":"make -j6 -f makefile_local.mk 2>build_error.log",
"options": {
"cwd": "${workspaceFolder}/"
},
"problemMatcher": []
},
{
"type": "shell",
"label": "build",
"dependsOrder": "sequence",
"dependsOn": [
"make_2",
"make_1"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": []
}
],
"version": "2.0.0"
}
- 環境變量配置, 上面launch.json 中配置了.env 文件的路徑
.env
LD_LIBRARY_PATH=./lib:/lib64
用途是: 如果我的程序需要依賴某個動態庫,比如,工作目錄下lib和lib64里面是需要的動態庫,可以在這里加入環境變量,可以是相對路徑,或者絕對路徑.
當前你也可以在系統環境變量里面設置
以上是 C/C++ 使用VScode的方法,如果對你有幫助,請點一個支持吧 _