简体   繁体   English

包含文件包含数组时的链接错误

[英]Linking error when including file containing array

I have the following code in my class object: 我的类对象中包含以下代码:

void Object::drawSurface()
{
   GLUnurbsObj *nurbSurface;

   nurbSurface = gluNewNurbsRenderer();
   gluNurbsProperty( nurbSurface, GLU_SAMPLING_TOLERANCE, 25.0 );
   gluNurbsProperty( nurbSurface, GLU_DISPLAY_MODE, GLU_FILL );
   GLfloat knots[26] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };

   gluBeginSurface( nurbSurface );
   gluNurbsSurface( nurbSurface, 26, knots, 26, knots,
        13*3, 3, &points[0][0][0], 13, 13, GL_MAP2_VERTEX_3 );
   gluEndSurface( nurbSurface );
 }

Also a .txt file is also included, which contains an array with all the points. 还包括一个.txt文件,该文件包含一个包含所有点的数组。

Everything works fine until I include my class object in any other class. 一切正常,直到我在其他任何班级中加入班级对象为止。 I then get this error: 然后我得到这个错误:

ld: duplicate symbol _points in openglscene.o and main.o
collect2: ld returned 1 exit status

The compiler means the symbol points[] which is declared in the txt. 编译器表示在txt中声明的符号points []。 I dont have a clue why this error emerges 我不知道为什么会出现此错误

That .txt file is directly or indirectly being included in at least two of your source files. .txt文件直接或间接包含在您的至少两个源文件中。 That is, from linker's point of view, it is defined twice. 也就是说,从链接器的角度来看,它被定义了两次。

What you must do is, in your header files, only say: 您必须做的是在头文件中仅说:

extern definition of points;

So for example, if it is int point[100] for example, you say: 因此,例如,如果它是int point[100] ,则说:

extern int point[100];

Then, in one and only one of the source files, you include the .txt file. 然后,在一个且只有一个源文件中,包括.txt文件。

Note: The same thing is true for any variable or function. 注意:对于任何变量或函数,这同样适用。 To test this, you could try defining a simple function in one of the headers and include it in two positions. 为了测试这一点,您可以尝试在其中一个标头中定义一个简单函数,并将其包含在两个位置。 You will get the same linker error for that too. 您也将为此获得相同的链接器错误。

You might also consider naming your file as ".h" instead of ".txt" ! 您也可以考虑将文件命名为“ .h”而不是“ .txt”! (Thats not the solution - just a suggestion) - Shahbaz already explained the solution. (那不是解决方案,只是一个建议)-Shahbaz已经解释了解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM