Hi I've split my project in 2 parts while each part is implemented by one. So some includes like <reg51.h> is done twice which resulted in error. So in order to avoid this I did as follows: #if !defined(REG51_h__INCLUDED_) #define REG51_h__INCLUDED_ #endif but it seems that this doesn't result in: #include <reg51.h> if it has not been included before. How should I solve it?!! I appreciate your attention in advance!!
Try this:
#ifndef __REG51_H_INCLUDED #include<reg51.h> #endif
#ifndef __REG51_H_INCLUDED #include<reg51.h> #define REG51_H_INCLUDED #endif
I should think that the #define should be made in the reg51.h file Normally, yes. In thsi case, reg51.h is third-party code, so you might not want to modify it, which can cause problems on upgrades. (Not that that would stop me :))
As a personal rule, whenever I create a header file I always put a define for the __FILENAME_H at the top of the file. If such is not the case for reg51.h and you don't wish to modify the reg51 file, the alternative suggestion given above will accomplish the goal of preventing multiple includes. -m
put a define for the __FILENAME_H at the top of the file A note of caution here: unless you're a compiler or runtime system implementor (i.e. in the context of this NG: unless you work at Keil) you're forbidden to use that kind of name. All macro and external names with two leading underscores are reserved for use by compiler makers. This may seem like an overly nitpicky detail, but it can bite you, and if it does, it'll be one heck of a debugging session to find it. So, for your own benefit, change to a pattern like PROJECT_FILENAME_H.
Good point - although I was not aware that it was a forbidden issue - the issue of a naming confict is always there. I avoid such conflicts myself by using a unique naming convention for my own files.
"I avoid such conflicts myself by using a unique naming convention for my own files." Yes - that is the whole point! The convention is that ames beginning with uderscores are reserved for compiler writers - so if you use them, you have no guarantee that any name you choose will be unique (ie, that your compiler won't have got there first!)
"...ames beginning with uderscores..." Hmmm... what's up with that 'n' key, then? Let's just try it again: "...names beginning with underscores..." Yes - that's better!
"I was not aware that it was a forbidden issue" ISO/IEC 9899:1990 Programming Languages - C Section 7.1.3, Reserved Identifiers: "All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use"
This is what I do: create a my_reg51.h #ifndef MY_REG51_H #define MY_REG51_H #include <reg51.h> #endif In your source, you #include <my_reg51.h> This way, you don't need to modify reg51.h Anh