开发者

C lib calling on global variable in another lib

开发者 https://www.devze.com 2023-04-06 03:37 出处:网络
I have two libraries, to simplify: libA.lib, libB.lib libA has function: void read(num,*val){ *val=Globalval[num];

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开发者_JAVA百科. 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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号