Keil Logo


Information in this article applies to:

  • C51 Version 5.50
  • C251 Version 2.14
  • C166 Version 4.02


I have written the following header file 'header.h' and included it in several .c source files:

#ifndef header
  #define header

  unsigned char x;


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.

  1. Move '#define header' out of the header file and into each .c source file except for one of them, placing the #define before the #include. For example:
    #define header
    #include "header.h"

    This ensures that the header file is only processed for one .c source file.

  2. Move your variable declarations into a separate .c source file, then create a new header file that simply declares the global variables as extern. This new header file can then be included in every .c source file without the need for any preprocessor directives.

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.


The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Saturday, July 9, 2005

Did this article provide the answer you needed?
Not Sure
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.