Macro Guard - Defining a Filename

Hi,

I'm curious about the syntax of noting a filename when using #ifndef and #define in a header file.

1
2
3
4
#ifndef _MY_FILE_H_
#define _MY_FILE_H_
// prototypes here
#endif 


Particularly the pre/post underscores... how do they relate to the actual file name? I assume that the _ before the last character denotes the extension, and previous _'s that aren't at the beginning of the file mark spaces in the file name?

I've seen various implementations of this:

1
2
3
_MY_FILE_H_
__MY_FILE_H
MY_FILE_H


or...

 
#define FILENAME "c:\\datafile.dat" 


What is the difference? Many thanks in advance,
Jack
Last edited on
Ah, I think I've seen the light.

The identifier after #ifndef and #define doesn't "link" to anything in particular, but is instead just a constant name uniquely identifies the header. The only place it will ever be referenced is in these two preprocessor statements in a header.

So, one could name these almost anything they wish, as long as they are the same.
You are correct, you can do some fun things with Macros.
Yep. It is important that they be unique, however, across all #include-able files, so choose wisely. I prefix all my stuff with DUTHOMHAS, since I'm the only one that (should) use that (as it is my name). For example, my Algorithm V Huffman compressor has the following following header

1
2
3
#pragma once
#ifndef DUTHOMHAS_HUFFMANV_HPP
#define DUTHOMHAS_HUFFMANV_HPP 

Hope this helps.
Thanks for the confirmation. I had figured that this would have to be unique within the entire project right after the actual meaning clicked, so yea, it would definately make sense to come up with a naming scheme for them that would be unlikely to be used by anyone else either.

In the case of:
#define FILENAME "c:\\datafile.dat"
... the FILENAME need not be unique, because where the <value> (c:\\...) is given, FILENAME will be replaced with <value>? Do I understand this correctly? So these two header file guards would be valid:

1
2
3
4
//header1
#ifndef FILENAME "c:\\header1.h"
#define FILENAME "c:\\header1.h"
...

1
2
3
4
//header2
#ifndef FILENAME "c:\\header2.h"
#define FILENAME "c:\\header2.h"
...


Although, carrying over from VB experience I wouldn't think it wise to hardcode anything such as a path and filename - just trying to gain some footing in the C world overall.

Cheers, thanks for the help.
Jack
Er, no. Include guards don't typically have meaningful values associated with them -- it is the macro itself that must be unique.

Your compiler should complain if you try to #define something twice, as you have above in the example for header1 and header2.

The usual case is as I have posted. Sometimes people will give the macro an associated value of '1', but there is really no need (at least that I can remember or discern).

1
2
3
4
#ifndef JLEACH_KABLITZER_H
#define JLEACH_KABLITZER_H 1
...
#endif 

Also, that #pragma once is understood by a good number of compilers, and so it is worth having in there. It will speed up compilation on large projects for those systems smart enough to recognize it.

Good luck!
I wasn't sure about applying the value.. thanks for the info. I had seen that posted in a forum so I was trying to figure out what it meant, but it doesn't look very useful (at this point in time for me, anyway), so I'll pretend I never saw it.

I also saw mention of #pragma once , along with a note stating that it's not recognized by all compilers, so my initial thoughts were to leave it out. I was not aware that it has effect on compilation efficiency.

That said, is there any Conditional Compiling available in C++? In VB, we can force certain code to not be compiled except under certain situations. Can something similar be done here? I had not tried #pragma, but if it's benificial over the standard #ifndef and #define guard, I would like to at least play around with it, but I'm also interested in maintaining ASCII/ISO coding standards.

I read a wiki article on this (http://en.wikipedia.org/wiki/Pragma_once), wondering how it reacted in cases where they #include guards as well as the #pragma once is there. I wasn't able to find a definitive answer, but it seems that as the compiler processes, if it sees #pragma once and already has this file loaded, it would skip the rest of that file entirely (thus not requiring processing of the #include guards (or the rest of the file) except for the first reference to the header)?

Again, many thanks for your help.
Since not all compilers support #pragma once you need to have at minimum the #include guards. But for those compilers that do support it, also use the pragma directive -- this pays big dividends in compilation speed when compiling large projects or those with many includes.

As for conditional compilation, this is it. #define something or not, and wrap the code in an #if... [[#elif ...] #else ...] #endif macro. Remember, compilation happens at a completely different time than when your code executes.

ISO coding standards only say what is legal and illegal to do. You are more interested in coding style. And there is no one true coding style. It is more important that you maintain clear, easy to read code.

Hope this helps.
Topic archived. No new replies allowed.