## Recommended Posts

Hi Guys, I'm just starting my C++ opengl adventure (after getting fairly far using Tao). I'm trying to load some extensions - my problem is that I obviously want to use the pointer to the extension in several classes - however, when I try to declare the variables globally i.e. in a header file - I get lots of "already defined" compile errors. I googled the problem and I have inserted inclusion guards, and attempted to make the variables "extern" the define them in a cpp file but i've had no success yet. Could someone please give me a step by step example of how to achieve this? So for example I want to enable multitexturing by defining the variables: PFNGLACTIVETEXTUREARBPROC glActiveTextureARB; PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB; PFNGLMULTITEXCOORD1DARBPROC glMultiTexCoord1dARB; PFNGLMULTITEXCOORD1DVARBPROC glMultiTexCoord1dvARB; ... ... etc So where do I put these declarations and how can I use the pointers in multiple classes? I havn't posted any of my inclusion guards etc as I may have messed them up - so i'm looking for a fresh perspective as it were. P.S getting the extensions loaded is not my problem - it's more of a C++ organization problem. Kind Regards, Chris

##### Share on other sites
This will do:

extensions.h:#pragma onceextern PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;extern PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB;extern PFNGLMULTITEXCOORD1DARBPROC glMultiTexCoord1dARB;extern PFNGLMULTITEXCOORD1DVARBPROC glMultiTexCoord1dvARB;extensions.cpp:#include "extensions.h"PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = NULL;PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = NULL;PFNGLMULTITEXCOORD1DARBPROC glMultiTexCoord1dARB = NULL;PFNGLMULTITEXCOORD1DVARBPROC glMultiTexCoord1dvARB = NULL;

##### Share on other sites
Since you're talking about this, I want to mention a way you can save lots of typing. By using the preprocessor, you can write up the list just once, and have it output all three lists you need (externs in header, definitions in source, and the get proc address stuff).

#define EXTENTIONS_LIST                                       act(glActiveTextureARB,       PFNGLACTIVETEXTUREARBPROC)       act(glClientActiveTextureARB, PFNGLCLIENTACTIVETEXTUREARBPROC) act(glMultiTexCoord1dARB,     PFNGLMULTITEXCOORD1DARBPROC)     act(glMultiTexCoord1dvARB,    PFNGLMULTITEXCOORD1DVARBPROC)(in header)#define act(x, y) extern y x;EXTENTIONS_LIST#undef act(in source)#define act(x, y) y x = NULL;EXTENTIONS_LIST#undef actvoid init_opengl(){ #define act(x, y) x = (y)wglGetProcAddress("x"); EXTENTIONS_LIST #undef act}

##### Share on other sites
You might find Organising Code Files in C and C++ a useful article.

##### Share on other sites

sample.h
#ifndef _SAMPLE_H#define _SAMPLE_H//Your Code#endif

This is a pretty vanilla header include guard, if it's been included then _SAMPLE_H will have been defined and the next time it comes around the #ifndef (if not defined) will skip the block. keep the define name consistent and you'll have no problems ( sample.h -> _SAMPLE_H, my_weird_header.h -> _MY_WEIRD_HEADER_H )

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628275
• Total Posts
2981740

• 10
• 11
• 17
• 10
• 9