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

发布于 2024-12-20 07:58:48 字数 842 浏览 3 评论 0原文

我的类对象中有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笔一画续写前缘 2024-12-27 07:58:48

.txt 文件直接或间接包含在至少两个源文件中。也就是说,从链接器的角度来看,它被定义了两次。

你必须做的是,在你的头文件中,只说:

extern definition of points;

因此,例如,如果它是 int point[100] 例如,你说:

extern 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:

extern definition of points;

So for example, if it is int point[100] for example, you say:

extern int point[100];

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.

是你 2024-12-27 07:58:48

您也可以考虑将文件命名为“.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文