Header in library

Hi,

I'm trying to understand how to create a library with the "Morse" example.

It's rather simple, but I would like to know why the "indef" is written with morse_h instead of morse.h??

I did not find the reason in the documentation or in the more general C tutorials.

Thanks.

...but I would like to know why the "indef" is written with morse_h instead of morse.h??

It is not "indef", it is "ifndef".

#ifndef can be write as #if not defined

It is written as morse_h because you aren't defining the file name, but rather an flag.

So, this code:

#ifndef morse_h
#define morse_h

/* code */

#endif

Can be read as:
If the flag morse_h is not defined, define it and make available the code inside the #if.
If the flag morse_h is already defined, then the code inside the #if won't be available.

This is to prevent errors from multiple includes,

This is called "Include Guard", seach for it.

Thanks for your answer, but this is not exactly what I wanted.

May be I did not explained correctly my question!

I know that this "ifndef" sequence (sorry for misspelling) is to prevent multiple declaration error, it is correctly explained in the tutorial.

What I did not get is why the header file is named Morse.h, and the ifndef is Morse_h.

I suppose that the underscore is interpreted "as a dot", but it looks strange.

It is explicit using the underscore, so it can't be confused with the file.

#ifndef Morse_h

Checks if the flag Morse_h is defined.
See, it doesn't check the if the file is defined, so the name of the flag must be different.

#define Morse_h

This define the flag Morse_h
Again, it must be different from the file name, because it would mean that you are defining the file, but you are just defining a flag.

The names are similar to indicate that this #if is related to the "Include Guard" of Morse.h.

It's a convention. You could call it "MorseDotH_FileExists" instead of "Morse_h" if that makes more sense to you. You can not use a period in the identifier because:

Only alphabetic characters, digits and underscore (_) are permitted in C++ language for declaring identifier.

aarg:
It's a convention. You could call it "MorseDotH_FileExists" instead of "Morse_h" if that makes more sense to you. You can not use a period in the identifier because:

Thanks for sharing this!

Just now I tested with the period in the Include Guard, and I got no errors while compiling and uploading. Maybe it's the Arduino IDE particular characteristic?

#ifndef morse.h
#define morse.h
#endif

#if morse.h
int a = 2;
#else
int a = 1;

#endif

result:

Arduino: 1.6.3 (Windows 7), Board: "Arduino Uno"

sketch_aug15a.ino:2:14: error: token "." is not valid in preprocessor expressions

sketch_aug15a.ino:5:5: note: in expansion of macro 'morse'

Error compiling.

Thanks for your comments.
Now that makes sense!

Have a good day.
Gerard.

This doesn't give me any errors:

# ifndef myLibrary.h
# define myLibrary_h

But this gives:

# ifndef myLibrary.h
# define myLibrary.h

Interesting!