コードをコンパイルすると、奇妙なエラーが発生します。エラーが発生している変数のすべてが「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
以下は変数です。
#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;
}