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

标签 c++ compiler-errors g++

<分区>

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

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

最佳答案

试试这个:

  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 声明在 header 中,但定义总是 在 c/cpp 文件中。 必须与程序中使用的任何 extern 声明的变量相匹配。

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

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

头文件

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

源文件

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

希望这至少有点道理。

关于C++ 头文件和链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13217067/

相关文章:

c++ - 指向成员的指针如何知道它绑定(bind)了哪个成员?

c# - mvc中只能使用赋值、调用、自增、自减、new对象表达式作为语句

c++ - 使用 gcc 或 clang 自动生成 header

c++ ->?= 运算符是什么意思?

c++ - 我们可以可靠地预先增加/减少右值吗?

c++ - 当链接计数为2时,有没有办法完全删除一个inode?

c++ - 我可以获得未处理(目标)C++ 异常的堆栈跟踪吗?

c++ - 为什么 Sprite 是wiz0;不等同于向导 wiz0();在我的代码中? C2228 错误

c++ - 为什么我的 vector 下标超出范围?

c++ - 如何根据类型相关类型专门化 C++ 模板类函数?