Atmel studio 7 issue with extern keyword

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

Hi guys, am using

Atmel studio 7 (Version: 7.0.1006).

RM/GNU C Compiler

 

I am really confused with "extern" keyword.

 

I want to access same variables between files.

 

So i had to use extern.

 

The real issue is that, when I build the program, them compiler displays error "redefinition of 'variable' "

There are times the compiler builds the program without any errors & most of the time it ends up in error.

 

What could be causing this issue?

 

surprise

 

Am I missing out something?

It works occasionally, but not always.

 

 

File <main.c>

 

int column1,column2;

 

File <test.h>

 

extern int column1,column2;

 

File <test.c>

 

void function()

{

column1=0;

column2=0;

}

 

This topic has a solution.

Silly beans are always silly
:)

Last Edited: Tue. Oct 11, 2016 - 12:52 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Does main.c include test.h?

Anyway, you should avoid using externs as you should strive to minimise globals. Use accessor functions and/or design your code better.

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

That is interesting.

smiley

I was really looking forward to minimize my main c code and I was running out of time.

 

Anyway the issue got resolved.
 

I initialized values in the same line

 

extern int a=50; (gives an error)

where as

extern int a=50; (works fine)

 

I also want to know if it causes any issues in future if I use extern?

sad

Silly beans are always silly
:)

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

You can't use an initialiser on an extern. I'd suggest having a good read of the K&R book on C.

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

Thank you for  the valuable advice.
smiley

I'll definitely take a l'll definitely take a look at that that book.

Silly beans are always silly
:)

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

It is important to understand the difference between a declaration and a definition.

 

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

 

And here are som more 'C' learning/reference materials: http://blog.antronics.co.uk/2011...

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

Good work.
Thank you for the support

:)

Silly beans are always silly
:)

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

Note that this is all Standard 'C' stuff - nothing specifically to do with Atmel Studio.

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...