溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

VSCode添加頭文件(C/C++)的案例分析

發(fā)布時間:2020-08-10 10:30:39 來源:億速云 閱讀:769 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了VSCode添加頭文件(C/C++)的案例分析,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

使用VSCode編譯C/C++時,會存在找不到頭文件的情況這時候需要設(shè)置兩個地方:

1.c_cpp_properites.json
2.task.json

以下是我修改的對應(yīng)的文件

{
  "configurations": [
    {
      "name": "Win32",
      "includePath": [
        "${workspaceFolder}/**",
        "${workspaceRoot}",
        "xxx/include"
      ],
      "browse": {
        "path": [
          "${workspaceRoot}",
          "xxx/lib"
        ]
      },
      "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
      ],
      "compilerPath": "xxx/gcc.exe",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "gcc-x64"
    }
  ],
  "version": 4
}
{
  "version": "2.0.0",
  "command": "g++",
  "args": ["-g","${file}","-Lxxx/lib","-Ixxx/include","-o","${fileBasenameNoExtension}.exe"], // 編譯命令參數(shù),添加-L,-I選項
  "problemMatcher": {
    "owner": "cpp",
    "fileLocation": ["relative", "${workspaceRoot}"],
    "pattern": {
      "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
      "file": 1,
      "line": 2,
      "column": 3,
      "severity": 4,
      "message": 5
    }
  }
}

附加上launch.json,參考網(wǎng)上的,鏈接找不到了,感謝原作者。

{
  "version": "0.2.0",
  "configurations": [
    
    {
      "name": "(gdb) Launch", // 配置名稱,將會在啟動配置的下拉菜單中顯示
      "type": "cppdbg",  // 配置類型,這里只能為cppdbg
      "request": "launch", // 請求配置類型,可以為launch(啟動)或attach(附加)
      "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",// 將要進(jìn)行調(diào)試的程序的路徑
      "args": [],  // 程序調(diào)試時傳遞給程序的命令行參數(shù),一般設(shè)為空即可
      "stopAtEntry": false, // 設(shè)為true時程序?qū)和T诔绦蛉肟谔?,一般設(shè)置為false
      "cwd": "${workspaceRoot}",// 調(diào)試程序時的工作目錄,一般為${workspaceRoot}即代碼所在目錄
      "environment": [],
      "externalConsole": true,// 調(diào)試時是否顯示控制臺窗口,一般設(shè)置為true顯示控制臺
      "MIMode": "gdb",
      "miDebuggerPath": "xxx\\gdb.exe",// miDebugger的路徑,注意這里要與MinGw的路徑對應(yīng)
      "preLaunchTask": "g++", // 調(diào)試會話開始前執(zhí)行的任務(wù),一般為編譯程序,c++為g++, c為gcc
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

vscode 添加頭文件路徑的方法

配置IntelliSense 

擴(kuò)展程序會根據(jù)當(dāng)前系統(tǒng)環(huán)境配置基本信息,因此有可能配置不完整,這時需要通過生成c_cpp_properties.json文件來配置缺少的信息:

ctrl+shift+P打開Command Palette,運行C/Cpp: Edit configurations...生成c_cpp_properties.json:

"includePath": [
        "${workspaceFolder}/**",
        "D:\\ite_sdk\\sdk\\**",
        "D:\\ite_sdk\\openrtos\\**",
        "C:\\ITEGCC\\*"

構(gòu)建應(yīng)用程序

如果要構(gòu)建應(yīng)用程序,則需要生成tasks.json文件:

Ctrl+Shift+P -> Tasks: Configure Tasks… -> Create tasks.json file from templates -> Others.

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享VSCode添加頭文件(C/C++)的案例分析內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI