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 }