Linker Error - MC90S08QE

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Linker Error - MC90S08QE

2,599 Views
OZ1
Contributor I
I am new to using Codewarrior and have been more than slightly annoyed at my present problem.  I am using processor expert with MC90S08QE setup and have created a header file that will hold variables to be used in various parts of the program.  For right now, there is only one variable.

unsigned char Send_SPI;

this program will loop in the main method until a periodic timer event occurs, then it will send out an spi packet.  My problem is that when i use the #include in my main.c  as well as in my events.c file, I get the Linker error L1818 Send_SPI duplicated in main.c.o and events.c.o

I guess my question is how do I just do a simple #include "myfile.h" and use the variable located in there without getting this linker error?
 
 
 
 
 
 
 
 
 
Added p/n to subject.


Message Edited by NLFSJ on 2008-04-08 04:34 PM
Labels (1)
0 Kudos
11 Replies

721 Views
OZ1
Contributor I
Thanks for the tips everybody.
0 Kudos

721 Views
allawtterb
Contributor IV
The normal method for this is to not declare the variables in the header file but at the top of your source file. Then in your header file you put an extern declaration and place guards.  Your source file looks like:
Code:
#include "SPI.h"/* Variable Declarations */unsigned char Send_SPI;...

 
Then your header file looks like:
Code:
#ifndef SPI_HEADER  // Header guard#define SPI_HEADER...extern unsigned char Send_SPI;....#endif

You can then include the header file where ever needed.
 
There is more than one way to do this and I have seen variables placed in the header file and to only include them if the source file calling it.  Something like this, the source file looking like:
Code:
#define SPI_SOURCE#include "SPI.h"....

 and header file:
Code:
#ifndef SPI_HEADER   #define SPI_HEADER.....#ifdef SPI_SOURCE// Source declarations here   unsigned char Send_SPI;   ...#else// External declarations here   extern unsigned char Send_SPI;#endif#endif

I would not suggest doing it this way though.

 
0 Kudos

721 Views
OZ1
Contributor I
Thank you for the response.  What is the reasoning for having to do it this way? 

I have used Atmel microcontrollers in the past and have typically just been able to use a standard #include and have access to the variables there.
0 Kudos

721 Views
allawtterb
Contributor IV
This could be the case for a GCC based compiler (are you using WinAVR?).  It appears that GCC by default places uninitialized variables in the common block which allows for multiple definitions.   To place the variable in the data block instead and force initialization you must use the nocommon attribute.  To force the nocommon attribute for all definitions use the -fno-common compiler option.  Note that this will cause a variable to be intialized to zero.   
0 Kudos

721 Views
OZ1
Contributor I
Yes, it was avr studios with Winavr.  Thanks for clarifying the difference between the two ways of doing it.


Message Edited by OZ1 on 2008-04-08 10:20 PM
0 Kudos

721 Views
JeffS_
Contributor I
I will tell you this, it has nothing to do with CW, this is what any other (except perhaps the one you have been using) "C" compiler would do.


0 Kudos

721 Views
JimDon
Senior Contributor III
He is correct that this has nothing to do with CW. What has been describes would cause a link error in most "C" systems, as you have doubly defined it.

Perhaps the reason you are doing this has to do with PE and the regeneration of the code.
You should not define things in the files generated by PE, as they could get lost later.
If you are using PE generated code, normally you pass in variables, and other variables you need can be defined in files you create. Look at the API that PE has generated and see if this is not the case - that it is un-necessary to define a variable in the PE generated code. Both the .f file and the.c file will document the APi.
 


0 Kudos

721 Views
OZ1
Contributor I
Basically what I was wanting to do is have a periodic interrupt, when it is triggered, it sets a variable. In the main loop, it watches that variable and when it sees that it has been set transmits the spi packet and resets the variable.
0 Kudos

721 Views
allawtterb
Contributor IV


OZ1 wrote:
Basically what I was wanting to do is have a periodic interrupt, when it is triggered, it sets a variable. In the main loop, it watches that variable and when it sees that it has been set transmits the spi packet and resets the variable.


You can include headers in PE generated files.  The problem is that if they get regenerated by PE the headers you included will have to be added again.
0 Kudos

721 Views
OZ1
Contributor I
I have been looking through the Standard settings, where do I find the -fno-common compiler option?
0 Kudos

721 Views
CompilerGuru
NXP Employee
NXP Employee
It's not a CodeWarrior option, it is a gcc option to behave like CW when I understand this right.
You can suppress the L1818 message in the linker message dialog, but instead I would suggest to only declare variables in the header file (with extern) and define them explicitly in a *.c file (or use some macro magic to behave as if you would have done this).

Daniel
0 Kudos