using variables from other *.obj files?!
I would like to compile my cpp files into separate object files
and then link them together, but I have global variables
that I have to use from several files. If I try define them in all of the cpp''s, linker errors "already defined in ***.obj" or
something like that.
Is there any way to do this. I''m not sure if extern or static keyword could help.
God saw all that he had made. Shit happens sometimes. --the sixth day.
// Defined in globals.hextern int Count;extern bool Done;...// Add Globals.h to each cpp file.#include "Globals.h"...
One of the CPP files has to decalare the variable but that''s the jist of it.
Regards,
Jumpster
Semper Fi
Regards,JumpsterSemper Fi
October 20, 2000 11:17 AM
Yes. The "extern" keyword is what you would need. Variables by default are local to the module they are declared in. Functions are global. To make a global variable you would declare it in every module like this:
extern int i;
You also must define it in one like this:
int i = 0;
Since variables are automatically local to every module you don''t need to use the ''static'' keyword, unless you declare it within a function and want it to have a longer life-time than the function. You can also make functions local to a module by declaring them static like this:
static void aFunction(int i);
And defining it like this:
void aFunction(int i) {
...
}
Well, I hope this clears things up.
Ok. Won''t be trying that again.
extern int i;
You also must define it in one like this:
int i = 0;
Since variables are automatically local to every module you don''t need to use the ''static'' keyword, unless you declare it within a function and want it to have a longer life-time than the function. You can also make functions local to a module by declaring them static like this:
static void aFunction(int i);
And defining it like this:
void aFunction(int i) {
...
}
Well, I hope this clears things up.
Ok. Won''t be trying that again.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement