PDA

View Full Version : Multiple files



sage
07-12-2000, 11:50 AM
I am trying to use OpenGL functions in separate .cpp files within one project (VC++). I need to call some GL functions in separate files after initializing everything in one file. By just including the gl.h in a separate file it is obviously not defining anything. Is there a way to pass or get a reference to the OpenGL stuff from the main .cpp to the other .cpp's? It would be devestating to what I am trying to
accomplish if it all has to be in a single file. Please help.

-Sage

07-12-2000, 12:09 PM
You may just include gl.h at the top of each file.....

Or you may have an app.h
and include gl.h in that file
and then include that in each file....

whatever you want to do

Galileo430
07-13-2000, 01:23 PM
I am a BIG fan of breaking programs down into smaller easyer to handle files. For example the NEHE triangle tutorial I broke down into 3 files. main.cpp init.cpp and gfx.cpp.

Remeber if you want to reference something in another file, be it a variable or a function. Declare it at the top of the file you want to use it in with the "extern" keyword in front.

For example:

MAIN.CPP
int X; //X in Main.cpp
BOOL InitGL //The Function Init GL
{
}

GFX.CPP
extern int X; //Allows you to use the same X that is in MAIN.cpp
extern BOOL InitGL; //Allows you to call InitGL in Main.CPP

***NOTICE***: Do not mix "static" and "extern", it will nuke the world as we know it!

Any questions?

nexusone
07-13-2000, 05:42 PM
This is something I have been working on
See my post on custom routines.

One thing has been touch on is that you have all the gl.h/glu/glut/etc. header files at the top of each program you create.

example you want to call a routine call triangle() that you wrote in graphics.cpp

In order to use it in your main program you must define it in the program you are calling it from.

//main.c
#include<gl/gl.h>

void triangle(); // defines the routine triangle.
.
.code stuff here
.
glTranslate();
triangle();

.
. more code stuff
.

// graphics.cpp

void triangle()
{
glBegin( GL_TRIANGLES );
.
.Draw a triangle.
.
glEnd();
return
}
Hope this helps.

EricK
07-14-2000, 04:02 AM
The extern keyword is for your global variables (not for functions). The key here is that you want to define and instantiate your global variables only once (the first time you use it) but you need to declare in each file that uses them. The purpose of declaring them is to tell the compile what type of variable it is. By preceding with the extern keyword you declare but do not define your variable.
For functions such as glScalef the definition is already in a library that you will link against. The declaration you take care of by including the header file.
You might want to look up the extern keyword in a C++ reference, I suggest getting Thinking in C++ by Bruce Eckel which can be downloaded at http://www.bruceeckel.com/ThinkingInCPP2e.html

sage
07-14-2000, 04:20 AM
EricK: I ordered the book, but it'll be a week. If all I have to do is include gl.h's, which I have done, then I get errors pointing to lines in the gl.h while linking. I was thinking that something was not getting defined, because when all the gl commands are in a single file it works fine.

Here are the errors if you are interested:
c:\vc98\include\gl\gl.h(1152) : error C2144:
1)syntax error: missing ';' before type 'void'
2)'WINGDIAPI' : missing storage-class or type specifiers
3)fatal error C1004: unexpected end of file found

I get them for every class (other than main.cpp) that calls a gl function.

-Sage


[This message has been edited by sage (edited 07-14-2000).]

[This message has been edited by sage (edited 07-14-2000).]

nexusone
07-14-2000, 04:59 AM
Have downloaded the latest version of the opengl library and (.h) files? or
it could be a error in how you are using the varibles from the .h file in your program.



Originally posted by sage:
EricK: I ordered the book, but it'll be a week. If all I have to do is include gl.h's, which I have done, then I get errors pointing to lines in the gl.h while linking. I was thinking that something was not getting defined, because when all the gl commands are in a single file it works fine.

Here are the errors if you are interested:
c:\vc98\include\gl\gl.h(1152) : error C2144:
1)syntax error: missing ';' before type 'void'
2)'WINGDIAPI' : missing storage-class or type specifiers
3)fatal error C1004: unexpected end of file found

I get them for every class (other than main.cpp) that calls a gl function.

-Sage


[This message has been edited by sage (edited 07-14-2000).]

[This message has been edited by sage (edited 07-14-2000).]

EricK
07-14-2000, 05:46 AM
Your error message means that the compiler does not know what a WINGDIAPI is (I'm not sure I do either, sounds kinda painful). If you go to line 1152 in gl.h (you can search for it but it is usually in c:\program files\visualstudio\vc???\include\gl) you will see that it has this declaration: WINGDIAPI void APIENTRY glAccum (GLenum op, GLfloat value);
To make it short, you need to include windows.h prior to the include of gl.h. What the compiler is saying is: "I don't know what the hell you put in front of the void but I wasn't expecting it, maybe you meant to have a ; there".
By the way, you can download Bruce Eckel's book if you want to look at it today.

Whittick
07-14-2000, 06:30 AM
I usually make a general header file for my programs cause I am to lazy to re-type the icludes everytime. In its simplet form it looks like this:

#ifndef __GENERAL_H__
#define __GENERAL_H__

#include <windows.h>
#include <gl/gl.h>
#include <GL/glu.h>

#endif


I name it general.h and just include general.h at the top of my files.

Just a suggestion
Chris


[This message has been edited by Whittick (edited 07-14-2000).]

sage
07-14-2000, 07:16 AM
I would like to thank everybody for there suggestions. I was able to make the errors go away by adding windows.h to all my header files. It seems strange that even though you don't use the windows.h that it has to be there for the gl.h . I guess it is just part of the learning process of C++ OpenGL.

Thanks again to everyone,

-Sage

Whittick
07-14-2000, 07:58 AM
Wait till you add some MFC and then windows.h becomes a problem. http://www.opengl.org/discussion_boards/ubb/smile.gif


Chris