Multiple definition error

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

Hello,

 

I have got the following files:

 

USART0_interrupt.h

USART0_interrupt.c

USART0_polling.h

USART0_polling.c

main.c

 

The files 'USART0_interrupt.c' 'USART0_polling.c' define the same functions. Even though I just include one header file via preprocessor directives in my main.c, I obtain the error 'multiple definition of ...'. If I remove one source file from the project, the code works. So why does the linker link a source to my program, even if it is not used?

 

Kind regards

 

Jimmy

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

As long as both *.c files are part of the "solution" then they will both be compiled and the linker will try to link them! 

Remove the .c and .h files of the one your not using.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

Jimmy11040 wrote:
I have got the following files

Did you get them from here:

 

AN_1451 - AVR306: Using the AVR UART in C on tinyAVR and megaAVR devices

  

As the application note explains, they give you code for both interrupt-driven and polled operation - you need to pick one.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Jimmy11040 wrote:
Even though I just include one header file via preprocessor directives in my main.c,

 

It doesn't matter what headers you include. If you link 'USART0_interrupt.c' and 'USART0_polling.c' within a single program, you will get multiple definition errors (provided the identically-named functions have external linkage).

Dessine-moi un mouton

Last Edited: Tue. Feb 16, 2021 - 09:05 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AndreyT wrote:
It doesn't matter what headers you include. 

Indeed.

 

The headers just provide declarations - the error says definitions.

 

http://c-faq.com/decl/decldef.html

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jimmy11040 wrote:
I just include one header file via preprocessor directives

If you really want to have both interrupt and polled code in the project, with the decision controlled by preprocessor directives, then you also need to add that control to your .c files; eg,

 

USART0_interrupt.c

#ifdef USART0_INTERRUPT
:
// your interrupt-driven code here
:
#endif

 

USART0_polling.c

#ifdef USART0_POLLED
:
// your polled code here
:
#endif

 

Jimmy11040 wrote:
why does the linker link a source to my program, even if it is not used?

How could it tell which one to choose - since they have identical names ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Feb 16, 2021 - 09:18 AM