C ++头文件和链接器错误

时间:2012-11-04 08:05:47

标签: c++ compiler-errors g++

编译代码时出错了。我想头文件没有正确链接,因为那些错误的变量中的每一个都已在'variables.h'中指定,我正确#include。奇怪的是,如果我评论在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

下面是variables.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;
}

3 个答案:

答案 0 :(得分:3)

试试这个:

  1. 打开你的variables.h头文件。
  2. 复制所有extern变量声明。
  3. 打开main.cpp文件。
  4. 粘贴从(2)。
  5. 复制的所有声明
  6. 在同一个main.cpp中删除每个声明中的关键字extern
  7. 保存所有文件。
  8. 查看extern的工作原理。有些东西告诉我你在学习中错过了。
  9. 好的,这已经涵盖了几千次,但对于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文件中的定义始终必须是您程序中使用的任何extern声明变量的匹配定义。

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

    <强>边栏 在您的变量已定义的c / cpp文件中,请确保将其初始化为正确的值。这是唯一可以做到的地方。你不能在任何extern声明中做到这一点。它只能在定义

    上完成

    标头文件

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

    源文件

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

    希望至少有点意义。

答案 1 :(得分:0)

extern float lookfromx;
extern float lookfromy;
extern float lookfromz;
extern float lookatx;
extern float lookaty;
extern float lookatz;

这些只是声明。您需要在程序中的某个位置定义这些变量(例如,在variables.cpp中)。要制作这些定义,您可以

  • 删除extern关键字

  • 添加初始化程序(=值;)

答案 2 :(得分:0)

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

  

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

Read more on the extern keyword