对构造函数和析构函数的未定义引用
undefined reference to constructors and destructors
我只看到过这是由于人们在构建时没有将他们的类对象链接在一起造成的,但我看到过,我不确定是怎么回事。
来源:
//test.cpp
#include "Point.h"
#include "Sphere.h"
#include "Scene.h"
int main(){
Sphere s;
Scene sc;
sc.img_plane.upper_left = Point(-1, 1, -3);
sc.img_plane.upper_right = Point(1, 1, -3);
sc.img_plane.lower_left = Point(-1, -1, -3);
sc.img_plane.lower_right = Point(1, -1, -3);
sc.width = 100;
sc.height = 100;
return 0;
}
错误:
test.cpp:(.text+0x17): undefined reference to `Sphere::Sphere()'
test.cpp:(.text+0x26): undefined reference to `Scene::Scene()'
test.cpp:(.text+0x192): undefined reference to `Scene::~Scene()'
test.cpp:(.text+0x1a1): undefined reference to `Sphere::~Sphere()'
test.cpp:(.text+0x1bf): undefined reference to `Scene::~Scene()'
test.cpp:(.text+0x1d3): undefined reference to `Sphere::~Sphere()'
Makefile:
CC = g++
SRC = ./src
BUILD = ./build
main : main.o Point.o colort.o Sphere.o Scene.o Ray.o HitRecord.o
$(CC) $(BUILD)/main.o $(BUILD)/Point.o -o $(BUILD)/main
main.o : $(SRC)/test.cpp
$(CC) -c $< -o $(BUILD)/main.o
Point.o : $(SRC)/Point.cpp $(SRC)/Point.h
$(CC) -c $< -o $(BUILD)/Point.o
colort.o : $(SRC)/colort.cpp $(SRC)/colort.h
$(CC) -c $< -o $(BUILD)/colort.o
Sphere.o : $(SRC)/Sphere.cpp $(SRC)/Sphere.h $(SRC)/colort.cpp $(SRC)/Point.cpp
$(CC) -c $< -o $(BUILD)/Sphere.o
ImagePlane.o : $(SRC)/ImagePlane.cpp $(SRC)/ImagePlane.h
$(CC) -c $< -o $(BUILD)/ImagePlane.o
Scene.o : $(SRC)/Scene.cpp $(SRC)/Scene.h
$(CC) -c $< -o $(BUILD)/Scene.o
Ray.o : $(SRC)/Ray.cpp $(SRC)/Ray.h
$(CC) -c $< -o $(BUILD)/Ray.o
HitRecord.o : $(SRC)/HitRecord.cpp $(SRC)/HitRecord.h
$(CC) -c $< -o $(BUILD)/HitRecord.o
clean :
rm $(BUILD)/*
test : main
$(BUILD)/main
场景构造函数与Sphere:相同
Sphere.h:
#ifndef SPHERE_H_
#define SPHERE_H_
#include "colort.h"
#include "Point.h"
#include "Ray.h"
class Sphere {
public:
Sphere();
virtual ~Sphere();
Point center;
double radius;
color_t diffuse, specular;
double intersection(Ray r);
};
#endif /* SPHERE_H_ */
Sphere.cpp:
#include "Sphere.h"
#include <math.h>
Sphere::Sphere() {
}
Sphere::~Sphere() {
}
...other class functions
在您的main
规则中:
main : main.o Point.o colort.o Sphere.o Scene.o Ray.o HitRecord.o
$(CC) $(BUILD)/main.o $(BUILD)/Point.o -o $(BUILD)/main
注意,尽管您提到Sphere.o是一个依赖项,但您并没有链接。您还需要将其包含在构建行中:
main : main.o Point.o colort.o Sphere.o Scene.o Ray.o HitRecord.o
$(CC) $(BUILD)/main.o $(BUILD)/Point.o $(BUILD)/Sphere.o -o $(BUILD)/main
您可能还想添加其他的,因为您将它们列为依赖项。
相关文章:
- 对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()' 错误的未定义引用