Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
"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. –  Nicol Bolas Feb 23 '12 at 17:35

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
I'm afraid that didn't work: I'm still getting the exact same errors. –  Avi Feb 23 '12 at 16:36
    
@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. –  Anton Feb 23 '12 at 18:56
    
A full rebuild worked. Thanks for the help, this is the first time my game engine ever compiled on Windows :D –  Avi Feb 23 '12 at 19:28
    
@Avi Congratulations :D –  Anton Feb 23 '12 at 20:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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