C++ linker error when trying to compile a second module using Code::Blocks

你说的曾经没有我的故事 提交于 2019-12-11 03:19:09

问题


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 totestClass::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)


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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



回答5:


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!



来源:https://stackoverflow.com/questions/3000736/c-linker-error-when-trying-to-compile-a-second-module-using-codeblocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!