1
\$\begingroup\$

I'm creating a game engine using OpenGL, and I'm initializing the extensions manually, i.e. this:

// .h file
PFNGLACTIVETEXTUREPROC glActiveTexture;

/* .cpp file, initialization function */
glActiveTexture = (PFNGLACTIVETEXTUREPROC) wglGetProcAddress("glActiveTexture");

However, when I compile, I get the following error every time I call an OpenGL extension function:

multiple definition of 'gl[some function]'

Why might this be? It reeks of an include loop problem, but I have include guards everywhere.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ "I'm initializing the extensions manually" Please stop doing this. Use an OpenGL loading library. It will make your life much easier, and it will have zero impact on your performance. \$\endgroup\$ Feb 23, 2012 at 17:35

1 Answer 1

2
\$\begingroup\$

Your .h file declares the variable glActiveTexture. Even with header guards, this means that when this header is included a new definition of the variable is created. To solve this, define the variable as extern and then define it in a single .cpp file of your choice.

I.e.

// .h file
extern PFNGLACTIVETEXTUREPROC glActiveTexture;

// .cpp file of your choice
PFNGLACTIVETEXTUREPROC glActiveTexture;

This tells the compiler to not create a new definition whenever it reads the header code, but to resolve it when linking. One of your .cpp files then provide this definition explicitly and the linker is happy.

\$\endgroup\$
4
  • \$\begingroup\$ I'm afraid that didn't work: I'm still getting the exact same errors. \$\endgroup\$
    – Publius
    Feb 23, 2012 at 16:36
  • \$\begingroup\$ @Avi Did you do a full rebuild? 'cause if you only edited the header and one .cpp file then the object files for the other .cpp files are still going to contain the old definitions. \$\endgroup\$
    – Anton
    Feb 23, 2012 at 18:56
  • \$\begingroup\$ A full rebuild worked. Thanks for the help, this is the first time my game engine ever compiled on Windows :D \$\endgroup\$
    – Publius
    Feb 23, 2012 at 19:28
  • \$\begingroup\$ @Avi Congratulations :D \$\endgroup\$
    – Anton
    Feb 23, 2012 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .