struct.h:
#ifndef _STRUCT_H #define _STRUCT_H int i; #endif
#include "struct.h" void main(void) { i = 1; }
Dear Mike Hwang, I think you need to brush up on your C before continuing on the embedded development path. Your problems stem from a lack of understanding about the C language. 1. Do *not* put code or define data in an include file. It is extremely bad style and error prone. 2. Always use include guards in your .h files but *don't* use leading underscores like the implementation header files do. Leading underscores followed by an uppercase letter or another underscore are reserved for use by the implementation. 3. If you want to share a global variable with another C module then define in it in one C module and extern it in that C module's .h file. Then, simply include that .h file in the other C modules that require knowledge of the global variable. E.g.
/* Foo.c */ int foo_var; /* Foo.h */ #ifndef FOO_H_INCLUDED #define FOO_H_INCLUDED extern int foo_var; #endif /* FOO_H_INCLUDED */ /* Bar.c */ #include "foo.h" int main(void) { foo_var = 12; return 0; }
You can include a header file which declares an identifier in the source file which defines that identifier; eg,
/* Foo.h */ #ifndef FOO_H_INCLUDED #define FOO_H_INCLUDED extern int foo_var; #endif /* FOO_H_INCLUDED */ /* Foo.c */ #include "Foo.h" int foo_var;