global variable across object files

L

luc wastiaux

Hello,
I need to share a global variable between functions, and for clarity
reasons, my code is split in different .c files that each compile into a
..o (object) which in turn are compiled together to form an executable.
What would be a good place to declare my global variable ? Can I put in
in a .h file that gets #included in all the .c's, (athough protected
against double inclusion) ?

thanks.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
Hello,
I need to share a global variable between functions, and for clarity
reasons, my code is split in different .c files that each compile into a
.o (object) which in turn are compiled together to form an executable.
What would be a good place to declare my global variable ? Can I put in
in a .h file that gets #included in all the .c's, (athough protected
against double inclusion) ?

Nope. It would be a bogus solution.

I suggest :
1 - get rid of this global. Use contexts (pointers to structures).
2 - if you can't, define it in a 'global.c' or 'data.c' source file.
3 - declare it in a header (say 'global.h' or 'data.h') with 'extern'. As
usual, use a protection against multiple inclusions.
4 - include the header in the source file where the variable is defined.
5 - include the header in the source files where the variable is used.

A clean way of using a dirty design...
 
E

Eric Sosman

luc said:
Hello,
I need to share a global variable between functions, and for clarity
reasons, my code is split in different .c files that each compile into a
.o (object) which in turn are compiled together to form an executable.
What would be a good place to declare my global variable ? Can I put in
in a .h file that gets #included in all the .c's, (athough protected
against double inclusion) ?

Are you sure you understand the distinction between
declaring a variable and defining a variable? See Question 1.7
in the comp.lang.c Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... which also answers your original question.
 
M

Malcolm

luc wastiaux said:
I need to share a global variable between functions, and for clarity
reasons, my code is split in different .c files
What would be a good place to declare my global variable ?
Declare the variable in the .c file where it makes most sense. In big
projects it usual to have a file that is devoted entirely to globals and
doesn't do anything else.

/* globals.c */

double g_aglobal = 0;

/* globals.h */

extern double g_aglobal;

/* foo.c */

#include "globals.h"

double afunction(void)
{
return g_aglobal / 2;
}

BTW globals in general are not a good idea, though most real programs will
have a few.
 
C

CBFalconer

Malcolm said:
Declare the variable in the .c file where it makes most sense.
In big projects it usual to have a file that is devoted entirely
to globals and doesn't do anything else.

/* globals.c */

double g_aglobal = 0;

/* globals.h */

extern double g_aglobal;

/* foo.c */

#include "globals.h"

double afunction(void)
{
return g_aglobal / 2;
}

BTW globals in general are not a good idea, though most real
programs will have a few.

You neglected to put a #include "globals.h" statement in
globals.c. Doing so is primarily for verification purposes.
 
D

Dan Pop

In said:
I need to share a global variable between functions, and for clarity
reasons, my code is split in different .c files that each compile into a
.o (object) which in turn are compiled together to form an executable.
^^^^^^^^
s/compiled/linked
What would be a good place to declare my global variable ? Can I put in
in a .h file that gets #included in all the .c's, (athough protected
against double inclusion) ?

Yes, but don't forget to also *define* it in one of the .c files.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top