Creating a header file.

What is the preferred method for creating a header file?

The IDE doesn’t let you “Save as” a *.h file but you can edit a *.h.

I’ve created a header file with Notepad, saved it with .h extension in the directory with the sketch. It takes a couple tries before the compiler is able to find the file. Is there. Is there a better way?

The tabs menu allows creating a new tab and then you can choose a filename

Doing a save as on the IDE actually creates a whole new folder with a copy of the entire project.

I sort of cheat. I fire up Eclipse where I have a project called scratch that I experiment with. I go to it and add a new class. This generates both the .cpp and .h files with all the includes, constructors, etc, all with perfect syntax. Then I copy those files to my sketchbook project folder and they'll open up on tabs in the IDE. They can be moved to the libraries when done.

The IDE isn't meant to distinguish between code and header files in your sketch. The tabs are essentially treated as one big file split up into sections for easier editing. Forcing standard C semantics WRT to inclusion and typical use of headers (prototypes, etc.) will instigate a fight between you and the IDE.

Handling of libraries is a little different, where .h and .cpp extensions mean something again.

SirNickity:
The tabs are essentially treated as one big file split up into sections for easier editing.

I think that only applies to any .ino files you create. All the .ino files in the sketch are concatenated and converted into a single .cpp file before compilation. However, any .h and .cpp files/tabs you add to the sketch are simply saved inside the sketch directory and copied directly into the compilation directory, and after that they get compiled and linked just as you'd expect.

I missed the little triangle off to the right on the Tab bar. It will create another tab and file which can be saved as a *.h file. Thanks to MarkT for pointing this out for me.

ted1191:
I missed the little triangle off to the right on the Tab bar. It will create another tab and file which can be saved as a *.h file. Thanks to MarkT for pointing this out for me.

That's alright (actually I'd never used it before, always edit in Emacs)

Hi people. am so new to arduino and C. Been copying and saving .h files and have been totally unsucessful in getting any of them to compile. h data copied from others sketches that work. And then getting them into the correct folder. (Now you see that I am a complete beginner. Working on grinbergs robot.

bobdds2690:
Hi people. am so new to arduino and C. Been copying and saving .h files and have been totally unsucessful in getting any of them to compile. h data copied from others sketches that work. And then getting them into the correct folder. (Now you see that I am a complete beginner. Working on grinbergs robot.

Read your PM, please.

Vaclav:
Read your PM, please.

It’s a forum…

Why would you hide the recipe to the secret sauce in a PM?

@Vaclav, I would think at this point you’d understand the benefit of sharing.