multiple definition of ""

Started by
12 comments, last by Jingo 19 years, 3 months ago
Quote:Original post by Raduprv
... and it's a pain in the ass to include all the needed header files ...


A small pain in the ass is better than a big pain in the ass. For example, what happens when you use the same file in two different projects? Which global header file does it include? Also, in a large project, you will have hundreds of header files. If every file has to include hundreds of header files, compiling will take forever.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Advertisement
We use some header files in our client and map editor. The problem is solved by using #defines.
For example, there is a define for the client, and one for the map editor, and the headers will load only the necesary stuff.
Others do it too, just look in windows.h
Quote:Original post by Inmate2993
In your header files, protect the file from double inclusion by using preprocessor definitions.
*** Source Snippet Removed ***

*** Source Snippet Removed ***

However, if you want everything to work from a single include file, you have to do this:

*** Source Snippet Removed ***

*** Source Snippet Removed ***

Its one of many ways to do it.


I would just like to mention a game programming tip I picked up from a book - if you have a larger project, you will want to do the same thing with the #ifndef, but Also in the source files. This will cut down compile time a lot because instead of pasting the whole header file it will skip it.

Here is an example:

header1.h:
#ifndef h1
#define h1
...
#endif

file1.cpp:
#ifndef h1
#include "header1.h"
#endif
...

.....................

file999.cpp:
#ifndef h1
#include "header1.h"
#endif
...

Instead of always pasting the entire header1.h file in every source file - then not even compiling it because it is already defined - you can use this method to only compile it once and include it once, thus saving a lot of compile time. One thing htey mentioned that is does look messy - but its valuable. You can also do the same thing for standard includes as well.

- Drew
Header guards should make no difference in the case that the OP described. All that would happen without them is he would get multiple declarations, not multiple definitions, which is fine.

This topic is closed to new replies.

Advertisement