编译代码时出现奇怪的错误。我猜头文件没有被正确链接,因为那些出错的变量中的每一个都已在我正确#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;
}