Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Advice on writing header files for C18 compiler

Status
Not open for further replies.

MrNobody

New Member
Hi.. I'm wondering, is there a correct way of writing header file..?
I look at some of the header files that are written, and tried to modify and compile a few..

An example is i open up C18's original delays.h. The first 2 lines are
Code:
#ifndef __DELAYS_H
#define __DELAYS_H
When I deleted the #define line, it still compile and run. Even if I replace "__DELAYS_H" with anything, it will still compile and run.. What does the 2 line mean and what is the reason of putting it there..?

Please advice.. thanks..
 
To 'see the problem',

1. delete the #define line.
2. include the header from 2 separate c file or perhaps from a single c source file.
3. compile
 
Also, don't forget the #endif at the end of the file:

Code:
#ifndef __MY_INCLUDE_H
#define __MY_INCLUDE_H

// . . .code. . .

#endif // __MY_INCLUDE_H

I'm just bringing this up because there was no #endif mentioned in the original post.

More information here: https://en.wikipedia.org/wiki/Include_guard


Torben
 
The O.P only deleted the 2nd line i.e. #define __DELAYS_H.
So the #endif line could be ignored.
 
The O.P only deleted the 2nd line i.e. #define __DELAYS_H.
So the #endif line could be ignored.

Yes, for this particular instance. What harm was caused by noting that #endif cannot be ignored, though? As I said, I only mentioned it for completeness.


Torben
 
I didnt say that a harm was caused :)
 
Hi.. I'm wondering, is there a correct way of writing header file..?

Well, there's certainly incorrect ways, so yes!

Also note that the compiler won't stop you from putting very foolish things in the header file, like actual code. That will probably compile and run fine but it defeats the organizational structure the header file was designed to create.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top