Maker Pro
Maker Pro

Including header files

Hi Guys,

Does anyone know how to use header files to simplify programming.

I'm using MPLAB and HI-TECH compiler.. programming in C.


I've managed to create my .h file with the C programming inside, so how do i include that in a general program?
 
I'm sorry, I don't do much C (or pay money for compilers), but maybe @chopnhack will see this and reply. He is becoming quite adept at rolling C code.:cool:

Hi Sadler, he is saying that tongue in cheek. I would rewrite the above statement and substitute adept with inept. For real coding help get with Supercap2F, I nominate him our coding guru at the moment.

Long and short, create your header file with definitions, make sure its in the same directory as your other files or you will have to point the compiler to it.
Include (pun intended :p ) a line in your main.c that looks like this:

#include "your_header_name_here.h"
 
Last edited:

hevans1944

Hop - AC8NS
@chopnhack : It's all relative. Maybe you aren't up there with @Supercap2F yet, but you are way above where I am as a C programmer.

Thanks for explaining for @Sadlercomfort how to use an #include statement.

If I ever start playing with some of those really sophisticated PICs you are dipping into, I guess I will have to learn to write programs in C or C++ or whatever the programming language du jour is. Abstraction and pointers help to keep programs organized, if not exactly optimized.

I have been avoiding C since about 1989, or thereabouts, when we used a real-time variant of Unix to control a re-build of a 1950s era six degree-of-freedom space-environment testing system, used to characterize classified overhead sensor platforms. That was almost a two-year task, but I had a genius at my disposal to write the C code while I concentrated on the new hardware. We couldn't have done the job without him, and I learned the value of a team effort to get big things accomplished.

I have a Pentium 4 that is getting a little long in the tooth running Windows XP Professional SP-3. One of these days I will move the application software and data files to a new machine I built a few years ago and convert this machine to Linux, probably the Ubuntu distro. Then I will take a stab at writing programs in C or C++. The really nice thing about Linux is it runs on just about anything Intel. For now, the C++ version used with the Arduino Uno is enough for me to putter with, although not having an operating system on the Arduino is a real PITA.
 
Include (pun intended :p ) a line in your main.c that looks like this:

#include "your_header_name_here.h"

Thanks guys and @chopnhack :D

I originally learnt assembly in University but converted to C at work. :confused:

You wont believe the similarities, once you learn the syntax its pretty straight forward.


I'll give this ago now :)
 
Header files should never contain code - Only Defines/Typedefs/Function prototypes/etc. Where you put your code is in .c files.

So, first right click on the Header Files folder of your current project and select: "New -> C Header File."
Project.JPG
Then fill in the "New C Header File" window. Now make a new .c source file in the "Source Files" folder with the same name as the header file you just created. In your main.c file add the statement: "#include "MyHeader.h" "(where MyHeader is the header file you just created). The header file is in quotes instead of these: <> because it's a local file to the project. Also add the same statement in the .c source file you just created.

Now, your going to need whats called a "inclusion guard" in your MyHeader.h file, so it isn't complied twice (since it's included in two .c files: main.c and MyHeader.c). Here's how a inclusion guard looks:
Code:
#ifndef MYHEADER_H
#define MYHEADER_H

// add all your Defines/Typedefs/Function prototypes/etc. here

#endif
And that's all. Put all of your code that you want in a separate file in the MyHeader.c file. All functions should be prototyped in the MyHeader.h file only, and all your #defines should be in there too.

When the compiler sees that there is a MyHeader.h file it will look at the source file folder for a matching .c file. When it sees it, it simply compiles it along with the rest of the code. So you don't need to put a "#include "MyHeader.c" " anywhere in your code.

If all that was kind of confusing, then have a look here: http://www.gamedev.net/page/resourc...amming/organizing-code-files-in-c-and-c-r3173
That's how I figured out how to do all this stuff, and you might want to read it even if you did understand my explanation. :p:D
Dan
 
Thanks Dan,

I don't quite understand the use of an inclusion guard.

Let me have a read through the entire tutorial :D I like to read slowly and from start to finish so it sinks in.. might take me a few days.

I'll let you know how I get on.


PS. Turns out my first attempt at using header files didn't really work (It contained code). :rolleyes:
 

Harald Kapp

Moderator
Moderator
The inclusion guard is there to prevent multiple definitions or redefinition of the same definition. This may otherwise happen if an include file needs to reference another include file or a C file #includes multiple include files that have the same definition.

The first line of the guard is
#ifndef MYHEADER_H // this line checks whether this include file has already been processed, in that case the name "MYHEADER_H" is defined and the rest of the definitions is skipped up to and including the line
#endif

The second line of the guard is
#define MYHEADER_H // this line defines the name "MYHEADER_H" so other #includes of this file will see it defined, see previous paragraph.

The rest between #define and #endif
// add all your Defines/Typedefs/Function prototypes/etc.
here means exactly what Dan has written

PS. Turns out my first attempt at using header files didn't really work (It contained code).
While it is not good style to have code within a header, it doesn't necessarily mean it won't work. The C preprocessor and the C compiler don't care as long as the resulting syntax of the C code (with all processed includes) is o.k.

I hope that helps a bit along your way.
 
Last edited:
Top