[ODE] Another question about header files and linking stuff

skjold@cistron.nl skjold at cistron.nl
Tue Feb 18 02:20:02 2003


> Hm. I didn't know things could be "un-included", how is that actually
> done? I thought h-files were included like it was cut-and-pasted into
> the file. Or is the whole drawstuff.h sort of defined inside a { bracket,
> and after the end bracket } all the variables and stuff are invalid?

No, no. It's not a matter of "un-including" anything. But you have to determine for each source file (c/cpp) independently which header files it needs. Thus, if drawstuff.cpp uses OpenGL functions and your own program uses OpenGL functions as well, then you always should make sure that both files have the gl.h header included. So you are right that you can regard a header file as being copy/pasted into your source file, but it's not right assuming if a header file is included in one source file, that it also affects another source file that doesn't include that header.


> What I was trying to do was to make a header file which was an "addition"
> to drawstuff, without changing drawstuff itself. The problem was that
> the functions in this header had to access stuff from within drawstuff
> and ODE, as well as GL, and had to be accessible from the main program.
> What happened was that it either worked with ODE and drawstuff/GL, OR
> from within my program. Come to think of it, what you say about
> including it in the drawstuff headers make sense now. But then I
> would have to modify drawstuff... oh well.

Actually, the compiler doesn't really care where it finds those declarations, as long as it knows about a function before it's invoked. What's important is that it makes sense to you where you put that stuff. It's good thinking to put your own additions into a separate file sothat drawstuff can be left unchanged. So, let's say you need ode, drawstuff and gl together with the stuff you added yourself, and you want to use that whole bunch in your program. You could create a new header file that looks like this:

#ifndef _MYHEADER_H_
#define _MYHEADER_H_

#include <gl.h>
#include <drawstuff.h>
#include <ode.h>

/* Add your own declarations here */

#endif

And then you can just include your own header file in the program that makes use of all this.


> > Indeed a C compiler can't compile C++ code. :P
> 
> Yes.. but why do the test_buggy source end with .cpp if it's not C plus
> plus?

C++ is a superset of C, that means (in theory) that everything that is valid in C is also valid in C++. It's not uncommon to write code that resembles mostly (or even strictly) C and still name your source file cpp. Also this can be a handy thing if you want to use other C++ code in your program, even though your own code is strictly C. And finally, there are subtle variants of C that are not always compatible, and using a C++ compiler tends to be a little easier on that issue.

Btw, if you compiled a cpp source file with a C++ compiler, then I'm not sure why you were having problems with <vector>.

Greets,
Mark