MULTIPLE PUBLIC DEFINITIONS ?

i write a global.h file:

#ifndef GLOBAL_H
#define GLOBAL_H

int nGlobal;
#endif

//file A.c
#include "global.h"
....

//file B.c
#include "global.h"
....

Build target 'Target 1'
compiling A.c...
compiling B.c...
linking...
*** ERROR L104: MULTIPLE PUBLIC DEFINITIONS
SYMBOL: nGLOBAL
MODULE: A.obj
*** ERROR L104: MULTIPLE PUBLIC DEFINITIONS
SYMBOL: nGLOBAL
MODULE: B.obj

oh,why the error do not happen when i write code "#include <reg51.h>" in several .c files.
please help me,thank you very much.

Parents
  • As a rule of thumb, a header may contain:
    Type definitions:struct Point { int x, y; };
    Function declarations:extern int strlen (const char*);
    Data declarations:extern int a;
    Constant definitions:const float pi = 3.14;
    Enumerations:enum Light {red, green, blue};
    Include directives:include <stdio.h>
    Macro definitions:#define VERSION 12
    Conditional compilation directives:#ifdef NDEBUG
    Comments:/*something*/

    Conversely, a header never contain:
    Ordinary function definitions:char get(char *p) {return *p++}
    Data definitions:int a;
    Aggregate definitions:short tbl[] = {1, 2, 3};

    from THE C++ PROGRAMMING LANGUAGE,Bjarne Stroustrup

Reply
  • As a rule of thumb, a header may contain:
    Type definitions:struct Point { int x, y; };
    Function declarations:extern int strlen (const char*);
    Data declarations:extern int a;
    Constant definitions:const float pi = 3.14;
    Enumerations:enum Light {red, green, blue};
    Include directives:include <stdio.h>
    Macro definitions:#define VERSION 12
    Conditional compilation directives:#ifdef NDEBUG
    Comments:/*something*/

    Conversely, a header never contain:
    Ordinary function definitions:char get(char *p) {return *p++}
    Data definitions:int a;
    Aggregate definitions:short tbl[] = {1, 2, 3};

    from THE C++ PROGRAMMING LANGUAGE,Bjarne Stroustrup

Children
More questions in this forum