链接器未看到我的类.cpp
My Class.cpp is not being seen by the linker
我正在尝试使用 VS Code 作为我的 IDE,使用"run"和"C++"扩展名在单独的标头和 cpp 文件中编写一个 C++ 类。
主.cpp
#include "Fan.h"
int main() {
Fan fan1;
return 0;
}
范·
#ifndef FAN_H
#define FAN_H
class Fan {
public:
Fan();
};
#endif
风扇.cpp
#include "Fan.h"
#include <iostream>
using namespace std;
Fan::Fan() {
cout << "Fan Class" << endl;
}
我似乎真的找不到任何明显错误的东西。我想知道这是否是VS代码的设置问题。
如果我将 main #include"Fan.h"更改为"Fan.cpp".cpp它会起作用,因此它让我认为代码有效但链接器设置不正确。
将不胜感激任何帮助!
编辑:好的,所以我在不同的IDE中尝试了代码,它起作用了。这与VS Code有关。这是错误:
[Running] cd "c:Users<USER>DesktopFan" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:Users<USER>DesktopFan"tempCodeRunnerFile
C:Users<USER>AppDataLocalTempcclMFKxO.o:tempCodeRunnerFile.cpp:(.text+0x57): undefined reference to `Fan::Fan()'
collect2.exe: error: ld returned 1 exit status
听起来IDE只是在编译main.cpp
。您需要找到正在编译main.cpp
的命令,并确保它也将fan.cpp
编译为 fan.obj
。您还需要确保将main.obj
和fan.obj
传递给链接器(链接器生成可执行程序、main.exe
或其他任何内容(。
这里涉及两个步骤:
- cpp -> obj(将每个源文件编译为匹配的对象文件(
- obj -> exe(将许多目标文件链接到可执行文件中(
我会
说制作一个CMakeLists.txt文件并将main.cpp和fan.cpp添加到add_executable部分。然后VS可以通过CMake处理和运行文件。
相关文章:
- 模板专业化可以进入我的.cpp吗?
- 反向功能超出了我的 cpp 程序的范围
- 为什么我的 cpp 数组大小是 4 的倍数?
- 为什么我的.cpp文件无法解析 .h 文件中声明的变量?
- 似乎无法将我的标题文件与我的CPP文件连接到链接列表中
- 为什么我的 cpp 程序在第一行后挂断
- 在我的 cpp 或头文件中找不到错误,类构建失败
- 为什么我的 cpp 代码显示分段错误
- 我正在收到错误的“ list :: ShowList”:功能风格的初始化器似乎是我的CPP文件中的函数定义'
- makefile删除我的.cpp文件
- F11 逐行调试只有我的.cpp
- kswapd0 使我的 cpp 程序 CPU 使用率低于 10%.如何克服它
- 有人可以解释为什么我的 cmake 文件无法构建我的 cpp 文件
- 如何在 DOXYGEN 文档中获取我的 .cpp 文件
- 如果 cout 包含在我的标头中,为什么在我的 cpp 中未定义?
- 我的cpp项目没有征求意见
- 提供的头文件不会 #include <string> ,也不允许更改。如何将其包含在我的 cpp 文件中?
- 如何在我的cpp程序中使用这个宏
- 我试图调用c++函数从c文件到我的cpp文件,但得到一些错误:
- 我无法让多个小程序与我的 Cpp 服务器通信