Vscode中无法运行自定义头文件?
最近在学习C++语言,按照教程配在Vscode中配置好了相对应的c++环境,正常运行都没有什么问题。
但是现在学习到了分文件编写函数,按照教程自己却运行不了:
sum.h:
#ifndef __SUM__
#define __SUM__
int sum(int a,int b);
#endif
sum.cpp:
#include "sum.h"
int sum(int a, int b)
{
int sum_add = 0;
sum_add = a + b;
return sum_add;
}
coco.cpp
#include <stdlib.h>
#include <iostream>
#include "sum.h"
using namespace std;
int main()
{
int n1, n2;
cout << "输入两个整数: ";
cin >> n1 >> n2;
cout << "sum is " << sum(n1,n2) << endl;
while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
cout << "HCF = " << n1 << endl;
system("pause"); // 防止运行后自动退出,需头文件stdlib.h
return 0;
}
但是运行过后却是这样的:
他们都是在同一个文件夹下,想问一下是因为我配置c++环境错误吗(已经尝试过了重启vscode和重启电脑,还是不行)
点开”问题“选项也没有报任何错误。
附带上我的4个.vscode配置文件:
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"D:\\MinGW\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\..\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include",
"D:\\MinGW\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\..\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include-fixed",
"D:\\MinGW\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\..\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\..\\..\\..\\..\\x86_64-w64-mingw32\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "D:\\MinGW\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": []
}
],
"version": 4
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGW\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe", /*修改成自己bin目录下的gdb.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "task g++"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "task g++",
"command": "D:\\MinGW\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe", /*修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D:\\VsCppProject", /*修改成自己放c/c++项目的文件夹,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"-std=c++17"
],
"options": {
"cwd": "D:\\MinGW\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin" /*修改成自己bin目录,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
},
"problemMatcher":[
"$gcc"
],
"group": "build",
}
]
}
settings.json
{
"files.associations": {
"iostream": "cpp",
"cfloat": "cpp",
"cerrno": "cpp"
}
}
提示很明显, 你根本没有编译sum函数的cpp文件.
最后一行加上
#include "sum.cpp"
sum函数名改一下,可能和库中的sum函数冲突了。
tasks.json里的"${file}",去掉
15行改为 "D:\\VsCppProject\\*.cpp",
【以下回答由 GPT 生成】
我可以帮助您解决在Vscode中无法运行自定义头文件的问题。请跟随以下步骤进行操作:
首先,请确保您已经正确配置了C++环境。在您的.vscode文件夹中,打开c_cpp_properties.json文件,并确认includePath配置中包含了您的MinGW安装路径,例如:"D:\MinGW\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\mingw64\bin"。如果路径不正确,请修改它。
接下来,请确保您的自定义头文件sum.h和sum.cpp位于与coco.cpp相同的文件夹中。根据您提供的信息,它们应该都位于同一个文件夹下。
确保您的头文件sum.h使用了防止重复引用的预处理指令。您已经正确地在sum.h中使用了#ifndef和#define指令,以及在文件末尾使用了#endif指令。
检查您的头文件sum.h是否在coco.cpp中正确引用。根据您提供的信息,sum.cpp中已经引入了sum.h,但是coco.cpp中并未包含头文件。请确保您在coco.cpp中正确引入sum.h,如下所示: c++ #include "sum.h"
最后,请尝试重新编译并运行您的程序。如果问题仍然存在,请尝试使用其他编译器或重新安装MinGW。
希望以上步骤能够解决您的问题。如果还有其他问题,请随时告诉我。