Sign in to follow this  
s3mt3x

C++ inclusion Headache

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 this post


Link to post
Share on other sites
This will do:


extensions.h:

#pragma once
extern 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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 act

void init_opengl()
{
#define act(x, y) x = (y)wglGetProcAddress("x");
EXTENTIONS_LIST
#undef act
}


Share this post


Link to post
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 )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this