Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extern variable in 2 libraries

1 view
Skip to first unread message

guddu

unread,
Jun 28, 2008, 8:03:05 PM6/28/08
to
Hi,
I am trying to share a variable in two libraries. These libraries are
build separatly and loaded dynamically.
Both the libraries share one header file ( say t.h ) .

In t.h I have declared
extern int flag;

in library 1 ,
I define it as follows :
int flag ;

in library 1, I am assigning
flag =1 ;

in library 2, I am trying to access 'flag' in different function
calls.
But I am getting inconsistent value of 'flag'

At some point I get flag = 0 and other places I get flag = 1;

Ideally if its correct extern variable should be accessible in entire
library 2.
Here either I am getting not externing it correctly and I am getting
junk value..

OR

extern is not supported across different libraries..which get build
separately.

I have tried other way round also ..ie defining
int flag ;

in library 2 .

Thanks in advance,
guddu
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Andrei Voropaev

unread,
Jul 4, 2008, 2:46:35 AM7/4/08
to
On 2008-06-29, guddu <vish...@gmail.com> wrote:
> I am trying to share a variable in two libraries. These libraries are
> build separatly and loaded dynamically.
> In t.h I have declared
> extern int flag;
> in library 1, I am assigning
> flag =1 ;
>
> in library 2, I am trying to access 'flag' in different function
> calls.
> But I am getting inconsistent value of 'flag'


It just means that access to your "flag" happens before the "flag" is
set to anything. A shared library can provide function "_init". This
function will be called right after the library is loaded, before any
other access to the library is done. So maybe you should use this
function to set the value of flag, or in the simple case just provide
initialisazion for the variable in declaration. Ie. int flag = 1;

--
Minds, like parachutes, function best when open

Jack Klein

unread,
Jul 4, 2008, 2:46:06 AM7/4/08
to
On Sat, 28 Jun 2008 19:03:05 -0500 (CDT), guddu <vish...@gmail.com>
wrote in comp.lang.c.moderated:

> Hi,
> I am trying to share a variable in two libraries. These libraries are
> build separatly and loaded dynamically.
> Both the libraries share one header file ( say t.h ) .
>
> In t.h I have declared
> extern int flag;

So far, you have posted this question into just about every general
purpose C and C++ programming group on the Internet.

In quite a few of these groups, you have been told two things, in some
cases both by the same respondent in the same reply. Namely:

1. This is a bad idea and you probably should not do it.

2. Neither the C nor C++ language defines the mechanics of libraries,
they are completely defined by the combination of your particular
compiler and operating system. This is especially true when it comes
to dynamically loaded libraries. So there is no C or C++ language
answer to your question, if you want to do this you need to ask in a
group that supports your particular compiler/OS combination to find
out how.

I agree quite strongly with the first piece of advice, It also seems
silly to have separate libraries when one of them requires that the
other needs to be loaded, to supply the global object.

Nevertheless, if you insist on doing this, stop multi-posting the
question to every C and C++ language newsgroup you can find. So far
it is off-topic in every one of them where I have seen it.

Instead, post it to a specific group that deals with your specific
compiler/OS combination. Based on the headers in your post,
news:comp.os.ms-windows.programmer.win32 should be a good choice.

> in library 1 ,
> I define it as follows :
> int flag ;
>
> in library 1, I am assigning
> flag =1 ;
>
> in library 2, I am trying to access 'flag' in different function
> calls.
> But I am getting inconsistent value of 'flag'
>
> At some point I get flag = 0 and other places I get flag = 1;
>
> Ideally if its correct extern variable should be accessible in entire
> library 2.
> Here either I am getting not externing it correctly and I am getting
> junk value..
>
> OR
>
> extern is not supported across different libraries..which get build
> separately.
>
> I have tried other way round also ..ie defining
> int flag ;
>
> in library 2 .
>
> Thanks in advance,
> guddu

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

Thomas Richter

unread,
Jul 4, 2008, 2:46:10 AM7/4/08
to
guddu wrote:
> Hi,
> I am trying to share a variable in two libraries. These libraries are
> build separatly and loaded dynamically.
> Both the libraries share one header file ( say t.h ) .
>
> In t.h I have declared
> extern int flag;
>
>
>
> in library 1 ,
> I define it as follows :
> int flag ;
>
> in library 1, I am assigning
> flag =1 ;
>
> in library 2, I am trying to access 'flag' in different function
> calls.
> But I am getting inconsistent value of 'flag'

Dynamic libraries are outside the scope of ANSI C, and are compiler-
and/or vendor specific. Thus, you are strictly speaking off-topic here.
You should consult your compiler manual how they are supported.

However, a probably less troublesome approach would be not to export the
variable, but a function giving access to the variable, and always call
this function to modify or read its value.

So long,
Thomas

Francis Glassborow

unread,
Jul 8, 2008, 10:18:23 PM7/8/08
to
Jack Klein wrote:
> On Sat, 28 Jun 2008 19:03:05 -0500 (CDT), guddu <vish...@gmail.com>
> wrote in comp.lang.c.moderated:
>
>> Hi,
>> I am trying to share a variable in two libraries. These libraries are
>> build separatly and loaded dynamically.
>> Both the libraries share one header file ( say t.h ) .
>>
>> In t.h I have declared
>> extern int flag;
>
> So far, you have posted this question into just about every general
> purpose C and C++ programming group on the Internet.
>
> In quite a few of these groups, you have been told two things, in some
> cases both by the same respondent in the same reply. Namely:
>
> 1. This is a bad idea and you probably should not do it.
>
> 2. Neither the C nor C++ language defines the mechanics of libraries,
> they are completely defined by the combination of your particular
> compiler and operating system. This is especially true when it comes
> to dynamically loaded libraries. So there is no C or C++ language
> answer to your question, if you want to do this you need to ask in a
> group that supports your particular compiler/OS combination to find
> out how.
>
I really do not think you read responses very well :)

1) There is a problem with dynamic libraries in that neither C nor C++
specifies how they shall behave. This is a real problem because the
rules are different for MS Windows and for Unix type OSs.

2) Clearly the flag you want should not be part of either library but a
static part of your program with values set in respect of which of the
dynamic libraries are currently resident.

3) The whole issue of using dynamic libraries is OS specific and so you
really do need to post to a newsgroup that is specific to programming
for the oS you are interested in.

0 new messages