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
 }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM