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

发布于 2024-09-04 15:02:25 字数 1095 浏览 5 评论 0原文

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

Main.cpp:

#include "test.h"

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

test.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

test.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)||在函数 main':| 中 *voip*\test\main.cpp |6|未定义的引用testClass::testClass()'| obj\Debug\main.o(.text+0x13c):voip\test\main.cpp|7|对 `testClass::~testClass()' 的未定义引用| ||=== 构建完成:2 个错误,0 个警告 ===|

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

So I'm trying to learn C++ and I've gotten as far as using header files. They really make no sense to me. I've tried many combinations of this but nothing so far has worked:

Main.cpp:

#include "test.h"

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

test.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

test.cpp:

#include "test.h"

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

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

What I'm trying to do is pretty simple, but the way the header files work just makes no sense to me at all. Code blocks returns the following on build:

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 ===|

I'd appreciate any help. Or if you have a decent tutorial for it, that would be fine too (most of the tutorials I've googled haven't helped)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

过潦 2024-09-11 15:02:25

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

在 Code::Blocks 中,转到菜单中的 Project>Add File 并选择您的 test.cpp 文件。确保已选中“发布”和“调试”复选框。

然后构建->重建

编辑:

以下提示可帮助您了解 IDE 在编译时在后台执行的操作。转到设置 ->编译器和调试器->全局编译器设置 ->其他设置,然后在编译器日志记录下拉框中选择完整命令行。现在,每当您构建时,gcc 编译器命令都会记录在构建日志中。每当 StackOverflow 上有人询问您使用的 gcc 命令行时,您都可以复制并粘贴构建日志中的内容。

Code::Blocks doesn't know that it has to compile test.cpp and produce an object file test.o (so that the latter may be linked together with main.o to produce the executable). You have to add test.cpp to your project.

In Code::Blocks, go to Project>Add File in the menu and select your test.cpp file. Make sure that both Release and Debug checkboxes are checked.

Then Build->Rebuild.

EDIT:

Here's a tip to help you see what the IDE is doing under the hood when compiling. Go to Settings -> Compiler and Debugger -> Global Compiler Settings -> Other settings and select Full command line in the Compiler logging drop box. Now, whenever you build, the gcc compiler commands will be logged in the Build Log. Whenever someone on StackOverflow asks you for the gcc command line you used, you can copy and paste what's in the Build Log.

无人问我粥可暖 2024-09-11 15:02:25

您设置标题的方式没有任何问题。您的错误发生在链接过程中。你的 gcc 命令行是什么?我的猜测是您只编译了 main.cpp,而忘记了 test.cpp。

There's nothing wrong with the way you've set up the header. Your error is occurring during linking. What's your gcc command line? My guess is that you're compiling just main.cpp, and forgot test.cpp.

苏大泽ㄣ 2024-09-11 15:02:25

您使用什么命令来构建?看来您没有在 test.cpp 中进行编译和链接,因此当 main.cpp 去查找适当的符号时,它找不到它们(链接失败)。

What command(s) are you using to build? It seems that you are not compiling and linking in test.cpp, so when main.cpp goes to look for the appropriate symbols, it cannot find them (link failure).

终弃我 2024-09-11 15:02:25

正如其他答案中所述,这是一个链接错误。像这样编译和链接:

g++ Main.cpp test.cpp -o myprogram -Wall -Werror

as said in the other answers, this is a link error. compile and link like this:

g++ Main.cpp test.cpp -o myprogram -Wall -Werror
差↓一点笑了 2024-09-11 15:02:25

关于头文件的一些(简短)信息 - .cpp 文件中的 #include 行只是指示编译器将该文件的内容粘贴到此时要编译的流中。因此,他们让您在一个地方(test.h)声明 testClass 并在多个地方使用它。 (main.cpp、someother.cpp、blah.cpp)。您的 test.cpp 包含 testClass 方法的定义,因此您还需要将其链接到最终的可执行文件中。

但是头文件并没有什么神奇之处,它只是为了方便而使用的简单文本替换,这样您就不必一遍又一遍地声明相同的类或函数。你已经(正确地)在那里得到了 #ifndef TEST_H_INCLUDED 的东西,所以如果你有 someother.h 其中 #includes test.h 和 main.cpp #includes test.h 和 someother.h,你只会得到testClass 声明的单个副本。

希望这有帮助!

Some (brief) information about header files too -- the #include line in your .cpp files just instructs the compiler to paste in the contents of that file into the stream-to-be-compiled at that point. So they let you declare the testClass in one place (test.h) and use it in many places. (main.cpp, someother.cpp, blah.cpp). Your test.cpp contains the definitions of methods of testClass so you need to link it into the final executable as well.

But there's nothing magic about header files, it's just simple text substitution used for convenience so that you don't have to declare the same class or functions over and over again. You've (correctly) got that #ifndef TEST_H_INCLUDED stuff in there so that in the chance you have someother.h which #includes test.h and main.cpp #includes both test.h and someother.h, you'll only get a single copy of the testClass declaration.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文