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
  • 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;
    }

    3. C source files must be compiled in to machine code if you want to run them. This code is typically placed in a relocatable object file, often with a .obj or .o extension. One or more object files are then passed on to the linker/locater which takes them and fixes up their relative addresses with absolute addresses. The linker also pulls object code from the C library at this stage too.

    Hope this helps,

    - Mark

Reply
  • 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;
    }

    3. C source files must be compiled in to machine code if you want to run them. This code is typically placed in a relocatable object file, often with a .obj or .o extension. One or more object files are then passed on to the linker/locater which takes them and fixes up their relative addresses with absolute addresses. The linker also pulls object code from the C library at this stage too.

    Hope this helps,

    - Mark

Children
  • 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;
    The advantage to doing this is that the compiler can warn you if the declaration doesn't match the definition