0

Sorry for what is probably an asinine question. I'm relatively inexperienced in C++ and I'm writing a small program with a header file. My cpp and h filenames have a period in them, for example: "file_ver_1.1.cpp" and "file_ver_1.1.h"

In my header file I have the following code

#ifndef FILE_VER_1.1_H_
#define FILE_VER_1.1_H_
... some code
#endif 

I get a warning from the compiler saying "extra tokens at end of #ifndef directive" for the first line and "missing whitespace after the macro name" for the define line. Is my header file name invalid because of the underscores and/or periods? I've read these two articles but I didn't really understand them and I'm still confused:

    http://gcc.gnu.org/onlinedocs/cpp/Macros.html#Macros
    http://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files

Thank you for your help!

navr91
  • 236
  • 2
  • 13
  • You don't actually have version info in your file names, do you? – JustSid Feb 05 '14 at 18:29
  • Seeing as this is a rather basic question, you might want to check out SO's [list of good C++ books](http://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO Feb 05 '14 at 18:33
  • 1
    You header file name is *not* invalid, but this really does not have anything to do with the name of the header file, just how you have written the include guard. – crashmstr Feb 05 '14 at 18:46
  • A macro name must be an identifier. Identifiers are not arbitrary strings; they may contain underscores, but they may not contain periods. Specifically, an identifier consists of a letter or underscore followed by zero or more letters, underscores, or digits. Identifiers starting with underscores are reserved for use by the implementation (though it's slightly more complicated than that). – Keith Thompson Feb 05 '14 at 18:48
  • @JustSide is version info in file names a no-no? – navr91 Feb 05 '14 at 19:05
  • @crashmstr does the include guard not have to match the header file name? – navr91 Feb 05 '14 at 19:12
  • 1
    @navr91 An include guard has to be unique to the file. If you used an MD5 hash of the filename (prefixed with a character in case it starts with a digit), it would work just as well. As long as you use a valid identifier unique to each header file, you can make your include guards anything you want. – Angew is no longer proud of SO Feb 05 '14 at 19:37

1 Answers1

2

As it is, your header file is indeed invalid (and even if it weren't, it woudln't be doing what you though it were). A period is not a valid identifier character, so the macro name is just FILE_VER_1.

When using include guard macros, you have to devise a scheme of "translating" filename characters which are not legal identifier characters. You could, for example, use _P_ to represent a period. So your include guard would then be:

#ifndef FILE_VER_1_P_1_H_
#define FILE_VER_1_P_1_H_

//...

#endif

How exactly you do that is up to you (any other convention is OK), as long as it produces a legal identifier. And you should be consistent.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455