Udit Gupta
Udit Gupta

Reputation: 3272

how to define a variable in one file and use it in another one

The question may be very basic but I am not getting clue anyways ...

I have two files ... and I am mentioning what I want to do .

file 1

...
j = data->alloc_len;
...

file 2

...
  for(i=0;i<j;i++)
...

Its clear from above I want to assign value to a variable in one file and want to use that value in other file.

I tried #include "file1.c" in file2.c but it is giving lot of re-declaration errors.

I tried creating a seperate header file which only have one line int j and then included it in both files using extern but no benefit again.Although I think header files are meant for where i can create and assign a value to a variable in one file and then this value can be propogated to all other files by including this one.

May be I am wrong but I need it soon so please help me ...Thnx in advance.

Limitation -

The value can be assigned only through file1.c because data structure is declared and defined here only.I can not provide a value to variable j in a header file .

EDIT :

Although I mentioned but I think I could not clear my question.I have tried using it header files.

For debugging purpose I tried this ..

sample.h

 extern int j;

file1.c

 #include "sample.h"
 int j=245;

file2.c

  #include "sample.h"
  printf("%d",j);

but its getting error .

Error I am getting is :

    Couldn't open module sample.so, tried:
sample.so: cannot open shared object file: No such file or directory
./sample.so.so: cannot open shared object file: No such file or directory
/usr/local/lib/sendip/sample.so.so: 
    cannot open shared object file: No such file or     
    directory
/usr/local/lib/sendip/sample.so: undefined symbol: j

*none of the file contains main function actually *

Actually it is a very large project and I am using Makefile and all files will be linked at run time.

In short,the execution could be understood as there is a main.c file which contains main which in turns call file1.c and which in turn calls file2.c

About descriptive names I would say they are just for showing here otherwise I am already using descriptive name.

Upvotes: 1

Views: 4458

Answers (3)

David Heffernan
David Heffernan

Reputation: 612904

#include "file1.c" is almost never the correct solution to a problem. Invariable you want to be declaring things in header files and then defining them in just a single source file.

file1.h

extern int j;

file1.c

#include "file1.h"
j = data->alloc_len

file2.c

#include "file1.h"
if (j>0)
    ...

Having said all of this, I would strongly council you to pass values as parameters rather than use global state. Global state is evil. Avoid it like the plague.

Upvotes: 0

AndersK
AndersK

Reputation: 36082

like this

in the c file you define the variable say file1.c you write

int j;

...

j = data->alloc_len;

a header for your file1.h would contain

extern int j;

this header you include in file2.c

#include "file1.h"

but i would suggest using a more descriptive variable name.

Upvotes: 1

Mat
Mat

Reputation: 206689

You could put this in a header file:

extern int j;

and only declare the "real" j in file1.c. If file2.c includes that header, then it can use variable j.

But, use descriptive variable names a the very least for globals. And you should avoid globals as much as you can, they are a liability in the long term, IMO.

(You could consider something like making a function in file1.c that returns that value. This has the advantage of assuring that j is controlled in file1.c, and only read in other places, limiting the complexity of understanding who "owns" that variable.)

Upvotes: 3

Related Questions