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

// .h file

/* .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.

  • 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\$ – Nicol Bolas Feb 23 '12 at 17:35

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.


// .h file

// .cpp file of your choice

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.

| improve this answer | |
  • \$\begingroup\$ I'm afraid that didn't work: I'm still getting the exact same errors. \$\endgroup\$ – Avi Feb 23 '12 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 '12 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\$ – Avi Feb 23 '12 at 19:28
  • \$\begingroup\$ @Avi Congratulations :D \$\endgroup\$ – Anton Feb 23 '12 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.