Home > Net >  Unexpected Redefinition Error in CCS #10056 in Code Composer Studio
Unexpected Redefinition Error in CCS #10056 in Code Composer Studio

Time:12-15

I have the problem, that in CCS I encounter unexpected redefenition errors if I include headers.

Minimal example:

// main.c
#include "test.h"

int main(void)
{
    init();
    return 0;
}

with

// test.h
#ifndef TEST_H_
#define TEST_H_

int var;
void init();

#endif /* TEST_H_ */

and

// test.c
#include "test.h"

void init()
{
    var=0;
}

I get

error #10056: symbol "_var" redefined: first defined in "./main.obj"; redefined in "./test.obj"

on compilation. I'm pretty sure this should work in any C using IDE. What do I miss?

CodePudding user response:

" I'm pretty sure this should work in any C using IDE". No it doesn't.

Every time you include test.h in a C file, the variable var is not only declared but also defined, hence the compilation error.

Include guards are not designed to avoid multiple definitions across translation units, but more to adress multiple inclusions of the same header file in a single translation unit.

See for example https://fr.wikipedia.org/wiki/Include_guard

The proper way to adress this issue is to only declare your variable in the header file:

extern int var;

and then define the variable only once in a C file (without extern).

int var;
  • Related