extern variable in 2 libraries

extern variable in 2 libraries

Post by gudd » Mon, 30 Jun 2008 09:03:05


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: XXXX@XXXXX.COM -- 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.
 
 
 

extern variable in 2 libraries

Post by Jack Klei » Sat, 05 Jul 2008 15:46:06

On Sat, 28 Jun 2008 19:03:05 -0500 (CDT), guddu < XXXX@XXXXX.COM >
wrote in comp.lang.c.moderated:


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,



--
Jack Klein
Home: http://www.yqcomputer.com/
FAQs for
comp.lang.c http://www.yqcomputer.com/
comp.lang.c++ http://www.yqcomputer.com/ ++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.yqcomputer.com/ ~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- 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.

 
 
 

extern variable in 2 libraries

Post by Thomas Ric » Sat, 05 Jul 2008 15:46:10


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
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- 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.
 
 
 

extern variable in 2 libraries

Post by Andrei Vor » Sat, 05 Jul 2008 15:46:35


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
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- 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.
 
 
 

extern variable in 2 libraries

Post by Francis Gl » Thu, 10 Jul 2008 11:18:23


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.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- 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.