Search code examples
c++qtopenglnurbs

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.

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


Solution

  • 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.