Hello, I have two c source files in my project, and they both need include hardware.h(register define) file. I try to include the .h file to both C file but the compilter says the memory overlap. I choice include .h file to the main C file ,the compiliter says another c file's register not define. How to avoid it , thanks a lot.
You are probably defining stuff in the header file. Your header file should contain only the extern declarations. It's probably a Linker error you're seeing - not a compiler error? This has been discussed often here before - including quite recently. Try browsing back a bit, or do a 'Search' Note that it's all standard 'C' stuff - nothing specifically to do with Keil. Therefore, any decent 'C' textbook should be able to help you.
example of my .h file
unsigned char xdata A_register _at_ 0x0010; unsinged char xdata B_register _at_ 0x0020;
try #include <absacc.h>
Sigh. First, those are still defining declarations and have no business being in an included header file, just like Andy said. Second, there are very clear examples in the manual illustrating this very thing; that is, objects having a defining declaration (even using the absolute variable location keywork "_at_") in a C source file, then their extern declarations in a header file. Summarizing, put those two lines in a single C file. Edit those lines that currently exist in your header (.h) file, delete the "_at_ 0xXXXX" suffix and prefix them with "extern". Simple, huh? Just like the manual shows!
"try #include <absacc.h>" Totally irrelevant to the question!
you must write .h files like this //my.h #ifndef __MY_H__ #define __MY_H__ unsigned char xdata A_register _at_ 0x0010; unsinged char xdata B_register _at_ 0x0020; #endif //__MY_H__
"you must write .h files like this..." Those "Include Guards" simply prevent you from #including the same header more than once; they will not help in this case. The example you show is still incorrect, as it still contains the definitions of the identifiers. Header files should only contain the extern declarations!!!
"'Include Guards' simply prevent you from #including the same header more than once" Note that this is a Good Thing! I am not denouncing the use of "Include Guards" - they are definitely worthwhile. I'm just saying that they have nothing to do with this particular problem!
yeah,you are right We must put the definition into the c source files,and declare them in the header files. //my.h #ifndef __MY_H__ #define __MY_H__ extrn unsigned char xdata A_register _at_ 0x0010; extrn unsinged char xdata B_register _at_ 0x0020;//unnecessary if the variables are not global #endif //__MY_H__ //My.c unsigned char xdata A_register _at_ 0x0010; unsinged char xdata B_register _at_ 0x0020; The include guard is necessary to avoid re-including.
Refer to the following knowledgebase article if you want to define and declare in a header file: http://www.keil.com/support/docs/1868.htm Jon
Still not quite right: do not put the _at_ in the header file - that just goes in the definition; only name & type are required in the declaration As I've said before, if you #include the header in the 'C' file, it will give you the advantage of allowing the compiler to warn you if there's any mismatch:
//My.c #include "my.h" // allows the compiler to "see" both declarations & definitions // and thus to report any mismatch! unsigned char xdata A_register _at_ 0x0010; // A message will be given if this definition // does not match the preceding declaration in // the header! unsinged char xdata B_register _at_ 0x0020;
Since we're already using the nonportable "_at_" language extension, we'll let the C++ and C99 "//" commenting style slide on by without any points deduction ;-)