1. 程式人生 > 實用技巧 >VS Code 中的C++程式碼如何引用自定義標頭檔案

VS Code 中的C++程式碼如何引用自定義標頭檔案

關鍵字: Visual Studio Code | C++ | 引用自定義標頭檔案 | 多檔案執行

  本文整理了Visual Studio Code中執行C++ 程式時,當main函式呼叫其他檔案中的方法(呼叫自定義標頭檔案x.h)的解決方案。如遇問題,歡迎交流。

  你也可以同時參考官方文件:https://code.visualstudio.com/docs/cpp/config-mingw

  筆者嘗試了數個百度到的辦法, 都沒有解決問題。希望筆者的辦法能幫到你。

Step1 官方文件中的線索

Your new tasks.json file should look similar to the JSON below:

 {
   "version": "2.0.0",
   "tasks": [
     {
       "type": "shell",
       "label": "C/C++: g++.exe build active file",
       "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
       "args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe
"], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": true } } ] }

  The command setting specifies the program to run; in this case that is g++. The args array specifies the command-line arguments that will be passed to g++. These arguments must be specified in the order expected by the compiler. This task tells g++ to take the active file (${file}

), compile it, and create an executable file in the current directory (${fileDirname}) with the same name as the active file but with the .exe extension (${fileBasenameNoExtension}.exe), resulting in helloworld.exe for our example.

  從以上描述中,我們可以瞭解到“ ${file}”指系統將編譯我們當前視窗的檔案,當我們要引入自定義標頭檔案的方法時,標頭檔案所對應的cpp檔案也是需要編譯的。我們再往下看。

Modifying tasks.json

  You can modify your tasks.json to build multiple C++ files by using an argument like "${workspaceFolder}\\*.cpp" instead of ${file}. This will build all .cpp files in your current folder. You can also modify the output filename by replacing "${fileDirname}\\${fileBasenameNoExtension}.exe" with a hard-coded filename (for example "${workspaceFolder}\\myProgram.exe").

官方很清楚地告訴我們執行多檔案程式碼的方法,即將${file}替換為"${workspaceFolder}\\*.cpp"

Step 2 實踐

  下面是我們的三塊程式碼

 // main.cpp
 #include "add.h"
 #include<iostream>
 using namespace std;
 int main() {
     int a=0;
     int b=add(a);
     cout << b << endl;
     return 0;
 }
 ​
 
// add.h
 #pragma once
 #include<iostream>
 using namespace std;
 int add(int);
 
// add.cpp
 int add(int i){
     i++;
     return i;
 }

  接下來我們看配置

task.json

{
     "version": "2.0.0",
     "command": "g++",
     "args": [
         "-g",
         "${workspaceFolder}/test/*.cpp", //要包含所有需要執行的cpp,包含自定義標頭檔案對應的cpp
         "-o",
         "${fileDirname}\\${fileBasenameNoExtension}.exe"
     ], // 編譯命令引數
     "problemMatcher": {
         "owner": "cpp",
         "fileLocation": [
             "relative",
             "${workspaceRoot}"
         ],
         "pattern": {
             "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
             "file": 1,
             "line": 2,
             "column": 3,
             "severity": 4,
             "message": 5
         }
     }
 }

  我們的所有cpp檔案都在當前開啟的資料夾CPP(workspaceFolder)下的test下,所以他們的路徑統一為${workspaceFolder}/test/*.cpp。所以你只需要在這裡把它更改為你需要執行的cpp,*.cpp表示所有的cpp,當然也可以把所有cpp逐一寫進來,用逗號分隔。如:

     "args": [
         "-g",
         "${workspaceFolder}/test/main.cpp", 
          "${workspaceFolder}/test/add.cpp", 
         "-o",
         "${fileDirname}\\${fileBasenameNoExtension}.exe"
     ], // 編譯命令引數

  到這裡,你的程式應該就可以跑起來了。

其他你可能需要的配置資訊

launch.json

{
     "version": "0.2.0",
     "configurations": [
         {
             "name": "C++ Launch (GDB)", // 配置名稱,將會在啟動配置的下拉選單中顯示
             "type": "cppdbg", // 配置型別,這裡只能為cppdbg
             "request": "launch", // 請求配置型別,可以為launch(啟動)或attach(附加)
             "targetArchitecture": "x86", // 生成目標架構,一般為x86或x64,可以為x86, arm, arm64, mips, x64, amd64, x86_64
             "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", // 將要進行除錯的程式的路徑
             "miDebuggerPath": "C:\\Program Files\\Cpp\\mingw64\\bin\\gdb.exe", // miDebugger的路徑,注意這裡要與MinGw的路徑對應
             "args": [
             ], // 程式除錯時傳遞給程式的命令列引數,一般設為空即可
             "stopAtEntry": false, // 設為true時程式將暫停在程式入口處,一般設定為false
             "cwd": "${workspaceRoot}", // 除錯程式時的工作目錄,一般為${workspaceRoot}即程式碼所在目錄
             "externalConsole": false, // 除錯時是否顯示控制檯視窗,一般設定為true顯示控制檯
             "preLaunchTask": "g++" // 除錯會話開始前執行的任務,一般為編譯程式,c++為g++, c為gcc
         }
     ]
 }

c_cpp_properties.json

 {
     "configurations": [
         {
             "name": "Win32",
             "includePath": [
                 "${workspaceFolder}/**"
             ],
             "defines": [
                 "_DEBUG",
                 "UNICODE",
                 "_UNICODE"
             ],
             "intelliSenseMode": "msvc-x64"
         }
     ],
     "version": 4
 }