extern not working

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

extern not working

3,670 Views
bazza
Contributor I
I am rebuilding an app from scratch after losing the source code due to my own stupidity. Thought I saved it on a cd but found that all I saved was a shortcut and since then have had to reinstall Windows.
I am attempting to use global variables and it is just not working.
I create a separate Globals.c file and put the required variables in that file then I intend to use the variables in a function from another file with extern.
The global vars are there OK and the app compiles but after using #include "Globals.c", I get 36 link errors like this:
Link Error : Action.c: Global Object 'speed' was already defined in File: 'Globals.c'. There are 36 vars.
If I comment out #include "Globals.c" I get a couple of hundred "undefined identifier" errors.
This seems a silly error as the object was not already defined in Globals.c but it was 'only' defined in Globals.c.
There is no other reference to Globals.c. One of the many vague and unhelpful errors thrown by Code Warrior.
If someone was able to have a look at the project that would be nice.
Labels (1)
0 Kudos
6 Replies

653 Views
bazza
Contributor I
Finally solved this problem. I had twelve variables among the 36 which I was attempting to define in the process and this is what was causing all the link errors.
The error would claim that I had already defined the variable in Globals.c which I had not. I was defining it in the file which was using it (as an extern) as I thought this was the correct method.
I noticed the difference in the Datebook example so I then defined the culprits in Globals.c instead of in the main file and everything works.
I still claim that the errors are really inappropriate.
Many thanks for your helpful comments.
0 Kudos

653 Views
bazza
Contributor I

Thanks Tallguy and Bigmac for your comments. Truly I really don't know what I'm doing.

I do have a good book on ANSI C which helps but all this linking business is a bit of a mystery.

I took on board what you said and faithfully put my external variables in the correct Global files etc and still got all the link errors.

So the next thing I did was have a closer look at the Datebook example.

I gradually transferred all my code over to the Datebook example then deleted all the files I didn't need until the Datebook example was 100% identical to my project and it compiled and ran without a hitch.

I then adjusted a few of the settings in my project to agree with Datebook but still I get the link errors, ie 36 errors in all which is the number of variables. I really don't know what else I can do, apparently nothing!

I have just renamed the Datebook example and now have my external variables exactly as I want them without the static symbol so I will just carry on from there and put this mystery in the too hard bin.

0 Kudos

653 Views
bazza
Contributor I

I finally stumbled on using static after seeing this used on an external variable in Datebook example.

After appending static to every variable all the link errors dissappeared.

I also found that I could simply insert these external variables with static at the head of the same segments as the functions and it appears that in this way I don't even have to declare them as extern from the functions. Amazing and so easy.

I admit this is a C problem but there is no guide on the use of external variables using extern in either Code Warrior or Palm documents.

I am happy to use this solution for now but I notice in Datebook there are some external variables that do not use the static symbol. That is still a mystery as I still get an error if I try to do that.

0 Kudos

653 Views
TallGuy
Contributor I


bazza wrote:

I finally stumbled on using static after seeing this used on an external variable in Datebook example.

After appending static to every variable all the link errors dissappeared.

I also found that I could simply insert these external variables with static at the head of the same segments as the functions and it appears that in this way I don't even have to declare them as extern from the functions. Amazing and so easy.

I admit this is a C problem but there is no guide on the use of external variables using extern in either Code Warrior or Palm documents.

I am happy to use this solution for now but I notice in Datebook there are some external variables that do not use the static symbol. That is still a mystery as I still get an error if I try to do that.






Please look up what you are doing here...

By making all variables static, every file/object that includes Globals.c has a local copy of that variable, and I'm pretty sure that's not what you want.

What you want to do is:

1. Add the file Globals.c to your project
2. Create a file Globals.h that references all those global variables as extern (so it contains all the extern definitions of the global variables in Globals.c
3. Where you now include Globals.c, you should include Globals.h instead
4. Link
5. See how it works like it should!

And please, read up on the proper use of C, as you don't really know what you're doing.
0 Kudos

653 Views
bigmac
Specialist III
Hello,
 
The following thread may be of interest, for a discussion about the definition and declaration of global variables.
 
Regards,
Mac
 
0 Kudos

653 Views
TallGuy
Contributor I


bazza wrote:
I am rebuilding an app from scratch after losing the source code due to my own stupidity. Thought I saved it on a cd but found that all I saved was a shortcut and since then have had to reinstall Windows.
I am attempting to use global variables and it is just not working.
I create a separate Globals.c file and put the required variables in that file then I intend to use the variables in a function from another file with extern.
The global vars are there OK and the app compiles but after using #include "Globals.c", I get 36 link errors like this:
Link Error : Action.c: Global Object 'speed' was already defined in File: 'Globals.c'. There are 36 vars.
If I comment out #include "Globals.c" I get a couple of hundred "undefined identifier" errors.
This seems a silly error as the object was not already defined in Globals.c but it was 'only' defined in Globals.c.
There is no other reference to Globals.c. One of the many vague and unhelpful errors thrown by Code Warrior.
If someone was able to have a look at the project that would be nice.





Provided you want to do it that way, I would:

1. Not #include Globals.c
2. Compile and link with Globals.c and #include the header file that defines the externs, or alternatively define your externs where you need 'em.

That ought to fix your problem.

Hope this helps,

Bas Vermeulen
0 Kudos