在编译它时显示未定义的引用
While compiling it showing undefined reference to
我正在使用一个崇高的文本编辑器和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
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用