包含包含数组的文件时出现链接错误
我的类对象中有以下代码:
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 );
}
还包含一个 .txt 文件,其中包含一个包含所有点的数组。
一切正常,直到我将我的类对象包含在任何其他类中。然后我得到这个错误:
ld: duplicate symbol _points in openglscene.o and main.o
collect2: ld returned 1 exit status
编译器意味着在txt中声明的符号点[]。我不知道为什么会出现这个错误
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.
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. I dont have a clue why this error emerges
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该
.txt
文件直接或间接包含在至少两个源文件中。也就是说,从链接器的角度来看,它被定义了两次。你必须做的是,在你的头文件中,只说:
因此,例如,如果它是
int point[100]
例如,你说:然后,在一个且仅一个源文件中,您包含
.txt
文件。注意:对于任何变量或函数来说都是同样的情况。要测试这一点,您可以尝试在其中一个标头中定义一个简单的函数并将其包含在两个位置。您也会得到相同的链接器错误。
That
.txt
file is directly or indirectly being included in at least two of your source files. That is, from linker's point of view, it is defined twice.What you must do is, in your header files, only say:
So for example, if it is
int point[100]
for example, you say:Then, in one and only one of the source files, you include the
.txt
file.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.
您也可以考虑将文件命名为“.h”而不是“.txt”! (这不是解决方案 - 只是一个建议) - Shahbaz 已经解释了解决方案。
You might also consider naming your file as ".h" instead of ".txt" ! (Thats not the solution - just a suggestion) - Shahbaz already explained the solution.