-
需要安裝的擴展 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的方法,如果對你有幫助,請點一個支持吧 _
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/273980.html
標籤:其他
上一篇:高仿花生殼客戶端程式(qt)
