(.text+0x0): multiple definition of `ui8ArRxTx'

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


  Hello

 

  I got to a halt and I can't go any further. I am porting a project written AS7 to Arduino. I get few errors saying the a variable is defined multiple times. It is only declared in one .h file like this:

uint8_t ui8ArRxTx[ui16GenBufSize];

 

  The specific error message is:

 

  There is not such variable in function SpiArCamR1. Here is the function:

uint8_t SpiArCamR1(uint8_t ui8RegAddr)
{
	uint8_t ui8A; // a temporary variable
	SPCR = (1 << SPE | 1 << MSTR);
	SPSR = 0;
	PORTD = PORTD & ~(1 << 7);

	ui8A = SPSR;
	ui8A = SPDR;
	SPDR = ui8RegAddr;

	while (!(SPSR & (1 << SPIF))) {}
	ui8A = SPDR; // read byte and discard
	SPDR = 0; // write dummy byte

	while (!(SPSR & (1 << SPIF))) {} // check if SPI received a dummy byte
	ui8A = SPDR;

	PORTD = PORTD | (1 << 7);
	
	return ui8A;
}

  And by the way, it also complains about ui8A that is initialized and never used. Is there a better way to read the SPDR (spi data register) and do nothing with that data ? Some programming languages allow

= SPDR

  Any help is well appreciated.

This topic has a solution.
Last Edited: Mon. Dec 23, 2019 - 03:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

angelu wrote:

 

  I got to a halt and I can't go any further. I am porting a project written AS7 to Arduino. I get few errors saying the a variable is defined multiple times. It is only declared in one .h file like this:

uint8_t ui8ArRxTx[ui16GenBufSize];

 

But how many times is this .h  file included in different .c/.cpp? Each of it counts as a new global variable with the same name.

 

Usual way is:

 

into .h file - declaration "there will be some super global variable somewhere" by using extern:

extern uint8_t ui8ArRxTx[ui16GenBufSize];

and in corresponding .c/.cpp there'll be actual variable:

uint8_t ui8ArRxTx[ui16GenBufSize];

 

Computers don't make errors - What they do they do on purpose.

Last Edited: Sun. Dec 22, 2019 - 06:31 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KIIV_cz wrote:

But how many times is this .h  file included in different .c/.cpp? Each of it counts as a new global variable with the same name.

 

  In two places. The reason is I have a variable that is used in the main.ino file and in one function that is is one .cpp file. Is this OK ? If I do not include that .h file in one of the locations it complains that the variable is not declared.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use guard statements in the header file 

 

#ifndef MYHEADER_H
#define MYHEADER_H
...
...
#endif

 

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, it's not OK, as you've got one instance created in ino and second instance is created inside cpp. That's why I mentioned solution - "extern".

 

If you need more details, there is crapload of it on the internet, like:  c++ multiple definitions of a variable

Computers don't make errors - What they do they do on purpose.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

  Thank you for both replies. However, none of those worked. Reading on Internet I understand that Arduino does not respond very well to preprocessor directives. I gave up and renamed my .cpp files as .ino . No need for any headers in these files and it works. Mine is a somehow off books situation in that I modify global variables inside functions in these files. I did include Peter's I2C library and it works.

 

  I still want to figure out how to read the SPDR when I actually don't need that data. Because I do not do anything with that data I get many warning.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

angelu wrote:
I still want to figure out how to read the SPDR when I actually don't need that data. Because I do not do anything with that data I get many warning.

 

SPDR;  // this will read SPDR and throw away the results

Letting the smoke out since 1978

 

 

 

 

Last Edited: Sun. Dec 22, 2019 - 11:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

digitalDan wrote:

SPDR;  // this will read SPDR and throw away the results

 

  Works like magic.