mugetsu
mugetsu

Reputation: 4398

C lib calling on global variable in another lib

I have two libraries, to simplify: libA.lib, libB.lib

libA has function:

void read(num,*val){
*val=Globalval[num];
...
}

libB:

void write(num,val){
Globalval[num]=val;
...
}

that is the gist of what I want to achieve. The two libs are included in my main project files and the functions are called individually. So how do I have this work out? If the two libs were of the same lib, a simply global variable would be all I need.

I'm using microsoft visual studios

Upvotes: 0

Views: 159

Answers (2)

nmichaels
nmichaels

Reputation: 50941

In one of the files, probably the write one (libB), put something like this:

int Globalval[SIZE];

In its header file, which should be included by libA, put this:

extern int Globalval[];

If your example really is what you're doing though, keep both functions in the same file, and call them from wherever you need to. In that case, you just put the function prototypes in the header.

Upvotes: 1

Matthew
Matthew

Reputation: 25763

You should be able to use EXTERN

in one of your libs header file:

extern int g_myGlobal;

in one of your libs cpp file:

int g_myGlobal;

You would then have to include the header file from one lib in the other (or any other file) that wants to use g_myGlobal

Upvotes: 0

Related Questions