2013-02-10 211 views
2

我只看到这是由于在构建时没有将它们的类对象链接在一起造成的,但我有,而且我不确定是什么原因。对构造函数和析构函数的未定义引用

源:

//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()' 

生成文件:

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.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 
+1

我在makefile中的任何地方都看不到'test.cpp'。你如何评价? – us2012 2013-02-10 22:29:47

+0

我忘了发贴,现在是在问题中 – AdamSpurgin 2013-02-10 22:30:32

+0

还有些遗漏;什么时候test.cpp得到编译?这是产生错误信息的那个人。 – 2013-02-10 22:32:42

回答

5

在你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 

您可能还需要添加其他,基于这样的事实,你列出它们的依赖关系。

+0

该死的,我知道它会像这样愚蠢的 – AdamSpurgin 2013-02-10 22:34:13

+0

看起来他们也需要'Scene.o'。 – juanchopanza 2013-02-10 22:34:16

相关问题