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.

Code composer gives linker errors when using legacy COFF

Other Parts Discussed in Thread: TEST2

Hi All,

I'm working on a project where i want to use the Bluetopia SDK, the lib files are compiled in the legacy COFF format. My project is currently using the eabi (ELF) format. when i switch the format to COFF and clean and rebuild the project i get a lot of errors. After a while i figured out the source but i don't know the answer.

I have some header files where i declare some chars like this (not the actual header file):

#ifndef HEADTEST_H_
#define HEADTEST_H_

char test2;

char test3;

#define false 0
#define true 1

#endif

I use these chars in muliple c files in my project,  when i compile whet ELF there is no problem but when i compile with COFF then i get the following errors:

Description Resource Path Location Type
#10056 symbol "test2" redefined: first defined in "./src/Project/hardware/Sensor/Sensor1.obj"; redefined in "./src/Project/logica/Startup/Start.obj" Project C/C++ Problem

Some how the linker gives redefinition errors, can someone help me with this? All sugestions are welcome!

  • You probably not only changing the output format but also creates a new or renamed Project\Configuration and the output of the old configuration (name) is still included into the new configuration. Either delete the complete output directory (configuration name) or exclude it from build.

  • Hi,

    by doing:

    #ifndef HEADTEST_H_
    #define HEADTEST_H_
    
    char test2;
    
    char test3;
    
    #define false 0
    #define true 1
    
    #endif

    you are basically defining global variables test2 and test3 in the header file. You should declare the global variables in header files (using extern keyword), and then define it in one of the c file.

    Please refer to here for some explanation of the difference between defining and declaring in C/C++:
    http://www.cprogramming.com/declare_vs_define.html

  • Leo Hendrawan said:
    you are basically defining global variables test2 and test3 in the header file. You should declare the global variables in header files (using extern keyword), and then define it in one of the c file.

    Missed this, and sounds more reasonable.

  • Thank you!

    I think i didn't clean my project when i tried extern declarations, after reading your reply is tried again and now it works fine! Thanks!

  • Leo Hendrawan said:
    you are basically defining global variables test2 and test3

    “char test2;” is an incomplete definition. It declares as well as defines test2 but without initializing it.
    Each compilation unit file that includes this header will contain a global variable test2, but due to the missing initialization, the linker will join them into one.
    If one of the compilation units also does an initialization, the compiler will create an explicit object for it. The linker will still join all uninitialized instances. But if two compilation units initialize it, you’ll get a conflict.
    The problem is, that two code parts may independently create a variable of the same name. If they do not initialize it, they will be joined and you’ll never notice (except for the fact that the code won’t work). Incomplete definitions even can appear multiple times. (you you may include two header files that both define a variable with the same name)
    Using the extern keyword, you tell the compiler that there is a variable of this name and type, but the compiler will only reference it. If it isn’t defined anywhere eels, you’ll get a linker error.
    Unfortunately, some compilers don’t like to see an extern declaration and later a definition.
    extern char test2; // from include file test.h
    char test2 = 0; // in test.c file that includes test.h
    will give an error.

     Some useful design rules:

    Variable declarations in header files should have the extern keyword
    The part of the extern declarations should be excluded from inclusion into the related .c file.
    E.g.

    Test.c:
    #define __TEST_C__
    #include “test.h”
    char test = 0;

    Test.h:
    #ifndef __TEST_H__
    #define __TEST_H__
    #ifndef __TEST_C__
    extern char test;
    #endif
    #endif

    Global variables should be explicitly initialized (to avoid accidental joining unrelated instances that have the same name)
    Variables only used in a .c file but not exported by the header file should be tagged with the static keyword (so they won’t be linked to other object modules, even if the name should be identical)

     

    It’s more effort, but at the end, it pays.

**Attention** This is a public forum