Defining Inside of a .h?

Started by
7 comments, last by demonkoryu 14 years, 8 months ago
Hi, I am making a program which uses header files. I use Dark GDK as my Library. In my header files I don't want to have to define Dark GDK for each and everyone of my header files for speed purposes. How do I make it so the Dark GDK library is only included once in my main code?
Advertisement
You could put your Dark GDK library header files into your precompiled header (stdafx.h)

Another way is to create a single header that includes all your library files and then just include this one rather then including all the individual ones.
I might be misunderstanding the question, but wouldn't inclusion guards make sure it was only included once? If any of the methods in each header file need access to the DarkGDK library, you need to include it in those header files. No different than including <iostream>, for example.

For that matter, I would imagine that DarkGDK has inclusion guards in its own header files already, meaning you can #include it in each and every file you write if you want, but the pre-processor will only add in the included code once.

Or is this more about not having to type the same thing multiple times?

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Here:

What I mean is that for example say I have a header called Include and a header called Globals. The Contents of Include are:

#include "DarkGDK.h"
#include "SC_Collision.h"

The contents of Globals contains:

#Include "Include.h"

In my Main.cpp file I have this header file:

#include "Include.h"

See the problem there? It has to redefine the already defined. My question is how do I fix it.
You don't. That's just how C and C++ work.
If I understand you correctly, you want to ensure that your stuff doesn't get included in case it is already included?
You have to use "include guards", it works like:

// in your includes.h#ifndef INCLUDES_H#define INCLUDES_H// do stuff here...// #ifndef INCLUDES_H#endif 


If you use Visual C++, you can also use
#pragma once// #pragma once does the same as the standard include guard below; it's // more efficient to compile with Visual C++, but the standard include guard // works for every compiler.#ifndef INCLUDES_H#define INCLUDES_H// do stuff here...// #ifndef INCLUDES_H#endif 
Ty Konfusius That was what I was looking for :)

If I use #pragma once does it go at the very top of the code?
Quote:Original post by Dragon_Lance
Ty Konfusius That was what I was looking for :)

If I use #pragma once does it go at the very top of the code?


What he's talking about is to prevent a header from being included more than once in a single translation unit.

There's no standard way to do what you're asking - each translation unit is its own seperate entity, so if you need some header you must include it. Now, some compilers have an option to force certain headers to be included in every TU, but you definetly should not be relying on that.
Quote:Original post by Dragon_Lance
Ty Konfusius That was what I was looking for :)

If I use #pragma once does it go at the very top of the code?


It shouldn't matter where exactly you place it; but it's the most common place to put it.

This topic is closed to new replies.

Advertisement