在编译它时显示未定义的引用

While compiling it showing undefined reference to

本文关键字:未定义 引用 显示 编译      更新时间:2023-10-16

我正在使用一个崇高的文本编辑器和GNU编译器来做这件事。 我已经在同一层次结构级别在这三个文件中创建了它。仍然不知道为什么它显示.....

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe C:UsersBIVASH~1AppDataLocalTempcc1guWsB.o:Class.cpp:(.text+0xc): undefined reference to `speak()'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:UsersBIVASH~1AppDataLocalTempcc1guWsB.o:Class.cpp:(.text+0x11): undefined reference to `jump()'
collect2.exe: error: ld returned 1 exit status```

第 .cpp 类

#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
speak();
jump();
return 0;
}

凯特·

#ifndef CAT_H
#define CAT_H
void speak();
void jump();
#endif

猫.cpp


#include <iostream>
#include "Cat.h"
using namespace std;
void speak(){
cout<<"mmmeeeeoooowwww!!!!";
}
void jump(){
cout<<"jump jump jump !!!!!";
}

当您要使用在不同编译单元上编写的函数时,您必须与其他单元的目标文件链接。

为此,仅包含其他单元的标头是不够的,因为它只为您提供函数的声明。为了达到定义,您应该将这两个单元编译在一起。例如,对于 foo.cpp 和 bar.cpp:g++ foo.cpp bar.cpp

在您的情况下,只需写:

g++ Class.cpp Cat.cpp