0

编译代码时出现奇怪的错误。我猜头文件没有被正确链接,因为那些出错的变量中的每一个都已在我正确#include的'variables.h'中指定。奇怪的是,如果我在 main.cpp 中注释掉使用变量的区域,则会在另一个文件 readfile.cpp 中弹出相同变量的其他一系列错误。下面是错误输出,以及我的 main.cpp 和 variables.h 代码。有任何想法吗?

g++ -c main.cpp 
g++ -c readfile.cpp 
g++ -c Objects.cpp 
g++ -o raytracer main.o readfile.o Objects.o
Undefined symbols for architecture x86_64:
  "_depth", referenced from:
      init()    in main.o
      readFile(char const*)in readfile.o
  "_diffuse", referenced from:
      readFile(char const*)in readfile.o
  "_emission", referenced from:
      readFile(char const*)in readfile.o
  "_filename", referenced from:
      init()    in main.o
  "_fov", referenced from:
      init()    in main.o
      initCamera(float*)in readfile.o
  "_height", referenced from:
      init()    in main.o
      readFile(char const*)in readfile.o
  "_lookatx", referenced from:
      init()    in main.o
      initCamera(float*)in readfile.o
  "_lookaty", referenced from:
      init()    in main.o
      initCamera(float*)in readfile.o
  "_lookatz", referenced from:
      init()    in main.o
      initCamera(float*)in readfile.o
  "_lookfromx", referenced from:
      init()    in main.o
      initCamera(float*)in readfile.o
  "_lookfromy", referenced from:
      init()    in main.o
      initCamera(float*)in readfile.o
  "_lookfromz", referenced from:
      init()    in main.o
      initCamera(float*)in readfile.o
  "_maxvertnorms", referenced from:
      init()    in main.o
      readFile(char const*)in readfile.o
  "_maxverts", referenced from:
      init()    in main.o
      readFile(char const*)in readfile.o
  "_shininess", referenced from:
      readFile(char const*)in readfile.o
  "_specular", referenced from:
      readFile(char const*)in readfile.o
  "_spherecount", referenced from:
      init()    in main.o
  "_spheres", referenced from:
      readFile(char const*)in readfile.o
  "_triangles", referenced from:
      readFile(char const*)in readfile.o
  "_tricount", referenced from:
      init()    in main.o
  "_trinormals", referenced from:
      readFile(char const*)in readfile.o
  "_trinormcount", referenced from:
      init()    in main.o
  "_upx", referenced from:
      init()    in main.o
      initCamera(float*)in readfile.o
  "_upy", referenced from:
      init()    in main.o
      initCamera(float*)in readfile.o
  "_upz", referenced from:
      init()    in main.o
      initCamera(float*)in readfile.o
  "_vertexcount", referenced from:
      init()    in main.o
  "_vertexnormcount", referenced from:
      init()    in main.o
  "_vertices", referenced from:
      readFile(char const*)in readfile.o
  "_vertnormals", referenced from:
      readFile(char const*)in readfile.o
  "_width", referenced from:
      init()    in main.o
      readFile(char const*)in readfile.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

下面是变量.h..

#include "vertexnormal.h"
#include "sphere.h"
#include "tri.h"
#include "trinormal.h"
#include "vec.h"

#include <string>
#include <vector>

using namespace std;

// width and height specify image size
extern float width;
extern float height;

// maximum depth for a ray (level of recursion)
extern int depth;

// the output file to which the image should be written
extern string filename;

// camera specifiations (should i put in a struct?)
extern float lookfromx;
extern float lookfromy;
extern float lookfromz;
extern float lookatx;
extern float lookaty;
extern float lookatz;
extern float upx;
extern float upy;
extern float upz;
extern float fov;

//***************************//
//  Geometry Specifications  //
//***************************//

// specifies the number of vertrices for tri specifications
extern int maxverts;

// specifies the number of vertices with normals for tri specifications
extern int maxvertnorms;

// pile of inputted vertices
// might need to #include glm file                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
extern vector<vec> vertices;

// pile of inputted vertices with specified normals
extern vector<vertexNormal> vertnormals;

// pile of inputted spheres
extern vector<sphere> spheres;

// pile of inputted triangles
extern vector<tri> triangles;

// pile of inputted triangles using vertices with specified normals 
extern vector<triNormal> trinormals;

extern int vertexcount;
extern int vertexnormcount;
extern int spherecount;
extern int tricount;
extern int trinormcount;

//**************************//
//  Materials Specifiations //
//**************************//

extern float diffuse[3];
extern float specular[3];
extern float shininess;
extern float emission[3];

这是我的 main.cpp,

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

#include "Objects.h"

using namespace std;

#include "readfile.h"
#include "variables.h"


void init() {
    cout << "Reading in scene file... \n";
    cout << "Image size has been set to a " << width << " x " << height << " output. /n";
    cout << "The maximum recursion depth has been set to " << depth << ". \n";
    cout << "The image will be output to " << filename << ".png. \n";

    cout << "The camera has been instantiated with the following properties: \n";
    cout << "\t POSITION: (" << lookfromx << ", " << lookfromy << ", " << lookfromz << ") \n";
    cout << "\t DIRECTION: (" << lookatx << ", " << lookaty << ", " << lookatz << ") \n";
    cout << "\t UP: (" << upx << ", " << upy << ", " << upz << ") \n";
    cout << "\t FIELD OF VIEW: " << fov << " \n";

    cout << "An amount of " << vertexcount << " vertices has been specified with a maximum of " << maxverts << " allowed. \n";
    cout << "An amount of " << vertexnormcount << " vertices with normals has been specified with a maximum of " << maxvertnorms << " allowed. \n"; 

    cout << "An amount of " << spherecount << " spheres have been specified. \n";
    cout << "An amount of " << tricount << " triangles have been specified. \n";
    cout << "An amount of " << trinormcount << " triangles with calculated vertex normals have been specified. \n";
}


int main (int argc, char * argv[]) {
    readFile(argv[1]);
    init();
    return 0;
}
4

3 回答 3

3

试试这个:

  1. 打开你的 variables.h 头文件。
  2. 复制所有extern变量声明。
  3. 打开你的 main.cpp 文件。
  4. 粘贴从 (2) 复制的所有声明。
  5. 在同一个 main.cpp中,从每个声明中删除关键字。extern
  6. 保存所有文件。
  7. 查找如何extern工作。有件事告诉我你在学习中错过了这一点。

好的,这已经在 SO what 中介绍了几千次,但是对于 OP:

声明变量的存在

// DECLARE myvar, an int variable. no storage has been set aside
//  this is simply telling the compiler this thing exists.. somewhere.
extern int myvar;

定义变量的存在

// DEFINE myvar, an int variable. storage *is* set aside here.
//  only ONE of these, by this name, can be in your global 
//  namespace in your program.
int myvar = 0;

传统上,extern声明位于标头中,但定义始终位于 c/cpp 文件中。程序中使用的任何 -declared 变量都必须有一个匹配的定义。extern

这如何适合您的情况您的 所有变量都在中声明variables.h,但无论如何都没有定义。通过告诉您将所有这些声明复制/粘贴到源文件中(任何都可以;我选择 main.cpp 因为它已经在您的项目中),然后删除该源文件中的extern关键字(不是标题),您就是基本上定义了它们都正式存在的地方。现在,在您的其他源文件中对 'ed 变量的所有引用extern终于在链接时有了一些关联。

边栏定义变量的 c/cpp 文件中,确保将它们初始化为正确的值。这是您唯一可以做到的地方。你不能在任何extern声明上这样做。它只能在定义上完成。

头文件

extern int myvar; // note: no initial value.

源文件

int myvar = 0; // note: initialized to zero (0)

希望这至少有点道理。

于 2012-11-04T08:20:43.627 回答
0
extern float lookfromx;
extern float lookfromy;
extern float lookfromz;
extern float lookatx;
extern float lookaty;
extern float lookatz;

这些只是声明。您需要在程序的某处定义这些变量(例如,在 variables.cpp 中)。要进行这些定义,您可以

  • 去掉 extern 关键字

  • 添加一个初始化器 ( = value; )

于 2012-11-04T08:09:45.080 回答
0

您的 variable.h 文件只是使您的变量可在全局范围内的其他文件中移植,但仍需要声明它们。确保在主文件中声明实际变量(初始化的常规方法)。

extern 关键字声明一个变量或函数,并指定它具有外部链接(它的名称在定义它的文件之外的文件中可见)。修改变量时,extern 指定变量具有静态持续时间(在程序开始时分配,在程序结束时释放)。变量或函数可以在另一个源文件中定义,或者稍后在同一文件中定义。默认情况下,文件范围内的变量和函数声明是外部的。

阅读有关 extern 关键字的更多信息

于 2012-11-04T08:15:22.197 回答