|ARM Technical Support Knowledge Articles|
Applies to: General Topics
Information in this article applies to:
I have written the following header file 'header.h' and included it in several .c source files:
#ifndef header #define header unsigned char x; #endif
When I build my project, the linker complains that x is being redefined, however, I thought the preprocessor directives used would ensure the file is only processed once.
The scope of the '#define header' is limited to each source file. So for each source file, header is undefined and the variable x is redefined. The linker then sees x defined in more than one object file and complains.
There are two solutions to this problem.
#define header #include "header.h"
This ensures that the header file is only processed for one .c source file.
For example, you may create a file called 'globals.c' containing:
unsigned char x;
Then create a header file called 'globals.h' containing:
extern unsigned char x;
Simply include the header file 'globals.h' in each .c source file.
Article last edited on: 2005-07-09 12:16:25
Did you find this article helpful? Yes No
How can we improve this article?