Saturday, 15 March 2014

Defining Global variable, Which is going to be used by multiple .c files -



Defining Global variable, Which is going to be used by multiple .c files -

i needed define several variables (global variables), going used many .c files. variables string array, file pointer, int etc.

situation : filex.h

different c files(filea, fileb, filec, filed) compiled -c alternative individually(filex.h have been included all) of files compiled 1 time again combining them. when these compiled individually no error pop up, when compiled in grouping (filea.o, fileb.o , filec.o), pop ups error. there way solve it. thought of solving extern seems pop error also.

any suggestion please ?

i don't want define variable in each .c file

i have string array don't want define them every file

i'm assuming multiple definitions error because header looks like

global_variables.h

int global_a = 10; int const global_b = 20;

what need is

global_variables.h

extern int global_a; extern int const global_b;

global_variables.c

int global_a = 10; int const global_b = 20;

edit: farther explanation:

now in other files need global variables need is:

other_file.c

#include "global_variables.h" void some_func(void){ global_a = 5; /* ready utilize */ }

other_awesome_file.c

#include "global_variables.h" /* , can utilize globals */

c header-files extern

No comments:

Post a Comment