Sign in to follow this  
Dragon_Lance

Defining Inside of a .h?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

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