This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

*** ERROR L104: MULTIPLE PUBLIC DEFINITIONS

struct.h:

#ifndef _STRUCT_H
#define _STRUCT_H

int i;

#endif

main.c:
#include "struct.h"
void main(void)
{
	i = 1;
}

when compiled and linked,error is showed as follow:

Build target 'Target 1'
compiling main.c...
linking...
*** ERROR L104: MULTIPLE PUBLIC DEFINITIONS
SYMBOL: I
MODULE: struct.obj (STRUCT)
Program Size: data=9.0 xdata=4 code=16
Target not created

What is the problem?
Note:no error if compiled in C compiler.

Parents
  • "If the only files in your project are main.c and struct.h, then where did struct.obj come from?"

    Maybe he compiled struct.h - if you add a header to a uVision Project, it will get compiled unless you uncheck the 'Include in Target Build' option for the file.

    "It certainly looks like two OBJ files are being linked together, and both of them contain a definition for 'i'."

    Yes, if he did compile struct.h he would end up with a struct.obj containing a definition of i!

    "It is safest to make all variable declarations in '.h' files use extern, and then only actually declare [sic] the variable (without extern) inside one (and only one) '.c' file."

    This is where we need to take great care to distinguish between definitions and declarations:
    A declaration simply informs the Compiler about an identifier; it creates no code and allocates no memory;
    A definition actually provides the code to implement a function, or allocates the storage space for a variable.

    Therefore, in general, declarations should go in headers and definitions in '.c' files - and multiple definitions in the same scope are not allowed.

    (BTW: This is all standard 'C' stuff; nothing specifically to do with Keil.)

Reply
  • "If the only files in your project are main.c and struct.h, then where did struct.obj come from?"

    Maybe he compiled struct.h - if you add a header to a uVision Project, it will get compiled unless you uncheck the 'Include in Target Build' option for the file.

    "It certainly looks like two OBJ files are being linked together, and both of them contain a definition for 'i'."

    Yes, if he did compile struct.h he would end up with a struct.obj containing a definition of i!

    "It is safest to make all variable declarations in '.h' files use extern, and then only actually declare [sic] the variable (without extern) inside one (and only one) '.c' file."

    This is where we need to take great care to distinguish between definitions and declarations:
    A declaration simply informs the Compiler about an identifier; it creates no code and allocates no memory;
    A definition actually provides the code to implement a function, or allocates the storage space for a variable.

    Therefore, in general, declarations should go in headers and definitions in '.c' files - and multiple definitions in the same scope are not allowed.

    (BTW: This is all standard 'C' stuff; nothing specifically to do with Keil.)

Children