gpt4 book ai didi

尝试使用 Code::Blocks 编译第二个模块时出现 C++ 链接器错误

转载 作者:搜寻专家 更新时间:2023-10-31 00:22:43 25 4
gpt4 key购买 nike

所以我正在尝试学习 C++,并且我已经学会了使用头文件。他们对我来说真的毫无意义。我已经尝试了很多组合,但到目前为止没有任何效果:

主要.cpp:

#include "test.h"

int main() {
testClass Player1;
return 0;
}

测试.h:

#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
class testClass {
private:
int health;
public:
testClass();
~testClass();
int getHealth();
void setHealth(int inH);
};
#endif // TEST_H_INCLUDED

测试.cpp:

#include "test.h"

testClass::testClass() { health = 100; }
testClass::~testClass() {}

int testClass::getHealth() { return(health); }
void testClass::setHealth(int inH) { health = inH; }

我想做的事情非常简单,但头文件的工作方式对我来说完全没有意义。代码块在构建时返回以下内容:

obj\Debug\main.o(.text+0x131)||In function main':|
*voip*\test\main.cpp
|6|undefined reference to
testClass::testClass()'| obj\Debug\main.o(.text+0x13c):voip\test\main.cpp|7|undefined reference to `testClass::~testClass()'| ||=== Build finished: 2 errors, 0 warnings ===|

如果有任何帮助,我将不胜感激。或者,如果您有一个不错的教程,那也很好(我用谷歌搜索的大多数教程都没有帮助)

最佳答案

Code::Blocks 不知道它必须编译 test.cpp 并生成目标文件 test.o(以便后者可以链接在一起使用 main.o 生成可执行文件)。您必须将 test.cpp 添加到您的项目中。

在 Code::Blocks 中,转到菜单中的 Project>Add File 并选择您的 test.cpp 文件。确保同时选中 Release 和 Debug 复选框。

然后Build->Rebuild

编辑:

这里有一个提示,可帮助您了解 IDE 在编译时的幕后工作。转到 Settings -> Compiler and Debugger -> Global Compiler Settings -> Other settings 并在 Compiler logging 下拉框中选择 Full command line。现在,无论何时构建,gcc 编译器命令都会记录在构建日志中。每当有人在 StackOverflow 上询问您使用的 gcc 命令行时,您可以复制并粘贴构建日志中的内容。

关于尝试使用 Code::Blocks 编译第二个模块时出现 C++ 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3000736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com