02 Dec 2013, 14:46
Generic-user-small

Pe Tisco (2 posts)

Hello all, I am having troubles when I try to compile a program. Bellow you can see the code I am trying to compile. I am using “CppUTest 3.5”, working on an Ubuntu VM (Eclipse CDT).

The file uC_Regs.h is used to simulate the microcontroller memory registers, so it is included in several header files. When this inclusion is done, the compiler shows me the error 28: multiple definition of.

The error only occurs when the code is compiled for testing purposes (gcc with cpputest harness), if I compile it with my microcontroler compiler (Production code only) the behaviour is correct.

What am I doing wrong?

Thank you so much in advance.

File: uC_Regs.h

#ifndef UC_REG_H_
#define UC_REG_H_

char RX_REG;
char TX_REG;

#endif /* UC_REG_H_ */

File: uartEvent.h

#ifndef UARTEVENT_H_
#define UARTEVENT_H_

#include <stdint.h>
#include "Buffer.h"
#include "uC_Regs.h"

void uartEvent(void);

#endif /* UARTEVENT_H_ */

File: uartEventTest.cpp

extern "C"
{
#include <string.h>
#include "uartEvent.h"
}
#include "CppUTest/TestHarness.h"

File: intEvent.h

#ifndef INTEVENT_H_
#define INTEVENT_H_

#include <stdint.h>
#include "Buffer.h"
#include "uC_Regs.h"

void intEvent(void);

#endif /* INTEVENT_H_ */

File: intEventTest.cpp

extern "C"
{
#include <string.h>
#include "intEvent.h"
}
#include "CppUTest/TestHarness.h"

Compiler output:

make all 
compiling uartEventTest.cpp
Linking Example_tests
objs/tests/uartEventTest.o: In function `TEST_uartEvent_uartEventPopString_Test::testBody()':
/home/Example/tests/uartEventTest.cpp:28: multiple definition of `RX_REG'
objs/tests/intEventTest.o:/home/Example/tests/intEventTest.cpp:37: first defined here
objs/tests/uartEventTest.o: In function `TEST_uartEvent_uartEventPopString_Test::testBody()':
/home/Example/tests/uartEventTest.cpp:28: multiple definition of `TX_REG'
objs/tests/intEventTest.o:/home/Example/tests/intEventTest.cpp:37: first defined here
collect2: ld devolvió el estado de salida 1
make: *** [Example_tests] Error 1
10 Dec 2013, 03:25
James2009_pragsmall

James Grenning (137 posts)

Is uC_Regs.h your code, or from your uC vendor?

This code would compile if you changed it to


extern char RX_REG; extern char TX_REG; —

By adding extern you declare the variables, allowing references to them.

Then I expect you will get a link error due to unresolved external references. Then you need a c file to define the variables.


char RX_REG; char TX_REG; —

By putting this code in a header file, you got multiple definitions of these variables.

11 Mar 2014, 10:17
Generic-user-small

Pe Tisco (2 posts)

Hello,

Sorry for my late answer.

I found the problem just after write this post and I forgot to write the solution found (your message describes it really well) here.

Thank you so much for for your time and for your support, it is nice to know that anyone can put here its doubts and get a quick a good answer.

Best regards.

  You must be logged in to comment