struct.h:
#ifndef _STRUCT_H #define _STRUCT_H int i; #endif
#include "struct.h" void main(void) { i = 1; }
"no error if compiled in C compiler." Well, of course there isn't: you can only get Linker errors from the Linker!! See my reply here: http://www.keil.com/forum/docs/thread1812.asp Your file struct.h contains a definition of i; presumably you are also #includeing it in another 'C' source file (struct.c?) so that file will also contain a definition of i - presto! multiple definitions! You need to define i in one file only (eg, main.c) and then just have an extern declaration in your header file. BTW: 'i' is a particularly bad name for a global variable!
I think the two statements don't do any work as they do in C compiler in struct.h:
#ifndef #define
"I think the two statements don't do any work as they do in C compiler" Pardon? I don't understand what you're trying to say! Those lines are a standard 'C' coding practice known as an Include Guard - their purpose is to prevent any problems if you (accidentally) #include the same file twice.
If the only files in your project are main.c and struct.h, then where did struct.obj come from? It certainly looks like two OBJ files are being linked together, and both of them contain a definition for "i". The "ifndef" syntax used here is usually used to protect against the case where a header file is included twice, maybe because another header file references it. It does NOT mean you don't have to use "extern". It is safest to make all variable declarations in ".h" files use extern, and then only actually declare the variable (without extern) inside one (and only one) ".c" file.
There aren't those two lines in any header files in INC directory of KEIL why?
View all questions in Keil forum