This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

ERROR #10056 symbol "..." redefined; first defined in /main.obj

So I'm having a bit of a problem here in my MSP430 coding:

I have 3 ".c" files and one .h file in my program here. My .h file has only declarations of functions (UART, Initilizations) which these 2 areas are split into 2 of the .c files (Uart.c & Initials.c) I then have my main file (Main.c) that includes the header file and the 2 other .c files. I cannot seem to find where the issue is as again. My .h only has declarations such as

void initial(void);

void ConfigAdc(void);

and so on. It is saying that these functions (and some others) are redefined and I only define them in the .c file they are linked too

  • Do you include the .h in your .c files as well as in main.c? Does your .h file have include guards such that it is not included multiple times?

    Include guards as follows:

    #ifndef HEADERNAME_H
    #define HEADERNAME_H
    
    
    .......header file contents
    #endif
  • Yes, my main includes the .h file and the 2 .c files and I do have an "ifndef" in my header file.

  • In your Header file you need to add extern; extern void initial(void); otherwise they will be declared in your other .c files again.

  • I just put extern on all the functions in the .h file. I'm still getting the same error. also before those happen i get an error saying "#10010 errors encountered during linking;"

    also, where should I put the .h file and .c files in the project folder?

  • Yea I'm still having the same issue.

  • David Tietz1 said:
    I then have my main file (Main.c) that includes the header file and the 2 other .c files.

    While some people (even those generating demo projects) have this habit, you should not include .c files into .c files.

    Each .c file should be an independent compilation unit and only include header files. Imagine what happens, if the compiler first compiles your main .c file with the included files, and then the two other .c file separately ( as they are .c files and therefore to compile) , and then the linker tries to link all three compiler outputs? Right, you'll get redefinition errors. :)

    Apparently you messed-up things when adding the other .c files to your project. (files that are to be included are not added to a project. They are 'added' implicitly by the #include directive)

  • Hi Jens,
    Your last response has solved my error #10056 problem somehow.
    You wrote "They are 'added' implicitly by the #include directive)".
    Did you mean EXPLICITLY by the #include directive?
    It seems to me the implicit addition occurs when creating the file.
    And that was my situation.
    I 'created' the file in Project Explorer not realising that it would automatically be included in the build.
    BUT - when I commented out the #include directive the build failed.
    When I removed the file from Project Explorer and un-commented the #include it built ok.
    What's happening there?

    Still a newb.
    Thanks,
    Dave
  • Well, you add the files explicitly to the including object file, which adds them implicitly to the build. Adding them explicitly to the project too will include the code twice, once in the main.obj, and once in another individual object file, which will cause the linker to complain when it tries to join both. Apparently, creating the file through the IDE includes adding the file as individual compilation unit to the project too. (as if you had said 'add source file to project') Which is convenient but conflicts with the said habit of including .c files into other .c files.
    I work with MSPGCC and use UltraEdit as project manager ad editor. Here, creating a file is independent from the compilation and link process. If I want it included in the build, I need to add it manually to the makefile.
    If your build fails when not including the .c files to the main.c, then there's something wrong. Probably you don't include msp430.h in each of them, or not all symbols you use in main.obj are declared in the header file. (Of course, each .c file needs to include declarations of variables or functions defined in other .c files)

    One other difference between including .c files or compiling them separately is the scope.
    If you declare a global variable as static, only code that is in the same .c file (or included by it) can see or access it.
    So if you have main.c, a.c and b.c, and in a.c and b.c you have a global variable 'count' and declare it as static, a.c and b.c will have their own, independent variable if compiled separately, while they will share the same when both are included into main.c. Also in the first case, main.c won't be able to access any of the two (even if you tell that they are there), while when including the two, main.c will be able to access the shared one. This is an important difference when re-.using code in different projects. This sharing can lead to almost untraceable erratic behavior.
  • Hi Jens,

    Thanks for the verification.

    Dave

  • In reply to david genge:

    IIRC you can simply delete a misguided post as long as nobody has replied to it. (You can still try, that's why I replied to my own post instead of yours)

**Attention** This is a public forum