尝试使用Code :: Blocks编译第二个模块时出现C ++链接器错误 [英] C++ linker error when trying to compile a second module using Code::Blocks
问题描述
Main.cpp:
#includetest.h
int main(){
testClass Player1;
返回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:
#includetest.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
主:|
testClass :: testClass()'|
* voip * \test\main.cpp
| 6 |未定义引用
obj\Debug\main.o(.text + 0x13c): voip \test\main.cpp | 7 |对testClass ::〜testClass()的未定义引用|
|| ===构建完成:2个错误,0个警告=== |
我很感激任何帮助。或者,如果你有一个体面的教程,这也可以(我已经使用Google的大部分教程都没有帮助)
Code :: Blocks不知道它必须编译 test.cpp
并产生一个目标文件 test.o
(这样后者可以与 main.o
连接在一起生成可执行文件)。您必须将 test.cpp
添加到您的项目中。
在Code :: Blocks中,转到<$ c在菜单中选择$ c> Project>添加文件并选择你的 test.cpp
文件。确保Release和Debug复选框都被选中。
然后 Build->重建
。
编辑:
这里有一个提示可以帮助您了解IDE在遮罩下做了些什么编译时。转到设置 - >编译器和调试器 - >全局编译器设置 - >其他设置
并在编译器日志记录
下拉框中选择完整命令行
。现在,无论何时构建,gcc编译器命令都将记录在生成日志中。每当有人在StackOverflow询问你使用的gcc命令行时,你可以复制并粘贴生成日志中的内容。
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)
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.
这篇关于尝试使用Code :: Blocks编译第二个模块时出现C ++链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!