473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Globals and Exports

I noticed that in C, functions in any module are automatically exported. So
that it's not possible to use the same function name in two modules (ie.
source files).

Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?

More seriously, variables declared at file scope also seem to be
automatically exported. But in this case, the compiler/linker doesn't warn
me that the same name is being used in two or more modules. (Is this what
the fuss is about with 'global variables'?)

Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)

-- Bartc
Jun 27 '08 #1
13 1138
On 5 May 2008 at 9:53, Bartc wrote:
I noticed that in C, functions in any module are automatically exported. So
that it's not possible to use the same function name in two modules (ie.
source files).

Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?
Check out your compiler's options. For example, gcc has quite a
sophisticated visibility model: in the simplest case, you can use
__attribute ((visibility("hidden"))) as a qualifier, or compile with
-fvisibility=hidden.
More seriously, variables declared at file scope also seem to be
automatically exported. But in this case, the compiler/linker doesn't warn
me that the same name is being used in two or more modules. (Is this what
the fuss is about with 'global variables'?)

Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)
static.

Jun 27 '08 #2

"Antoninus Twink" <no****@nospam.invalidwrote in message
news:sl*******************@nospam.invalid...
On 5 May 2008 at 9:53, Bartc wrote:
>More seriously, variables declared at file scope also seem to be
automatically exported. But in this case, the compiler/linker doesn't
warn
me that the same name is being used in two or more modules. (Is this what
the fuss is about with 'global variables'?)

Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)

static.

That simple..

--
Bartc
Jun 27 '08 #3
Bartc wrote, On 05/05/08 10:53:
I noticed that in C, functions in any module are automatically exported. So
that it's not possible to use the same function name in two modules (ie.
source files).
Your C text book should have told you this. If not you need to reread it
and/or replace it.
Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?

More seriously, variables declared at file scope also seem to be
automatically exported.
Yes.
But in this case, the compiler/linker doesn't warn
me that the same name is being used in two or more modules.
It is not required to. Some can be made to however, so you should read
the documentation for your implementation.
(Is this what
the fuss is about with 'global variables'?)
High coupling making it hard to understand programs. You have to read
every function (or you search facilities on the entire code base) to
find out when and how the variables are changed and where they are used,
then you find you have a problem because you cannot change one piece of
code without breaking lots more.

The larger the project the larger the problem.
Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)
Look up the many uses of static in your text book.
--
Flash Gordon
Jun 27 '08 #4
Bartc wrote:
I noticed that in C, functions in any module are automatically exported. So
that it's not possible to use the same function name in two modules (ie.
source files).

Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?

More seriously, variables declared at file scope also seem to be
automatically exported. But in this case, the compiler/linker doesn't warn
me that the same name is being used in two or more modules.
Turn up warninglevels in your linker.
Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)
Read your C book... :-) Keyword static.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Jun 27 '08 #5
Antoninus Twink wrote, On 05/05/08 11:12:
On 5 May 2008 at 9:53, Bartc wrote:
>I noticed that in C, functions in any module are automatically exported. So
that it's not possible to use the same function name in two modules (ie.
source files).

Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?

Check out your compiler's options. For example, gcc has quite a
sophisticated visibility model: in the simplest case, you can use
__attribute ((visibility("hidden"))) as a qualifier, or compile with
-fvisibility=hidden.
So what is wrong with static? Why suggest a non-standard extension when
a feature the language has had in all implementations for years is
sufficient.
>More seriously, variables declared at file scope also seem to be
automatically exported. But in this case, the compiler/linker doesn't warn
me that the same name is being used in two or more modules. (Is this what
the fuss is about with 'global variables'?)

Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)

static.
After all, you know about it.
--
Flash Gordon
Jun 27 '08 #6
On 5 May 2008 at 11:12, Flash Gordon wrote:
Antoninus Twink wrote, On 05/05/08 11:12:
>On 5 May 2008 at 9:53, Bartc wrote:
>>I noticed that in C, functions in any module are automatically exported. So
that it's not possible to use the same function name in two modules (ie.
source files).

Check out your compiler's options. For example, gcc has quite a
sophisticated visibility model: in the simplest case, you can use
__attribute ((visibility("hidden"))) as a qualifier, or compile with
-fvisibility=hidden.

So what is wrong with static? Why suggest a non-standard extension when
a feature the language has had in all implementations for years is
sufficient.
Because they do different things. If you're making a shared library,
then you might want to have variables and functions that are globally
visible within your library, but don't pollute the namespace for users
of your library. Gcc's __attributes can achieve that.

Without extensions, you have only two choices: either make a variable
static, and so visible only within a single translation unit, or else
make it globally visible everywhere. Compiler extensions can provide
finer-grained visibility control, which seems to be what the OP had in
mind: when you link your shared library, all uses of a symbol throughout
the library files are resolved, but that symbol isn't put into the table
of externally-visible symbols in your .so file.

Jun 27 '08 #7
Antoninus Twink wrote, On 05/05/08 14:00:
On 5 May 2008 at 11:12, Flash Gordon wrote:
>Antoninus Twink wrote, On 05/05/08 11:12:
>>On 5 May 2008 at 9:53, Bartc wrote:
I noticed that in C, functions in any module are automatically exported. So
that it's not possible to use the same function name in two modules (ie.
source files).
Check out your compiler's options. For example, gcc has quite a
sophisticated visibility model: in the simplest case, you can use
__attribute ((visibility("hidden"))) as a qualifier, or compile with
-fvisibility=hidden.
So what is wrong with static? Why suggest a non-standard extension when
a feature the language has had in all implementations for years is
sufficient.

Because they do different things. If you're making a shared library,
<snip>

Which is not what the OP was asking about. The OP make clear in what he
posted that by modules he meant source files. This is actually quoted by
you immediately before your response.
--
Flash Gordon
Jun 27 '08 #8
On May 5, 10:53*am, "Bartc" <b...@freeuk.comwrote:
I noticed that in C, functions in any module are automatically exported. So
that it's not possible to use the same function name in two modules (ie.
source files).

You can't have two functions with the same name, correct. (I changed
your wording because it could have meant that you can't call a
function from another translation unit).

Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?

More seriously, variables declared at file scope also seem to be
automatically exported.

The linkage of a function or object can be either "extern" or
"static". You are right in thinking that the default is "extern", but
beware of the exception to the rule: if the object is const, then the
default becomes "static". I.e. the following two global object
definitions are the same:

int const i = 5;

static int const i = 5;

But in this case, the compiler/linker doesn't warn
me that the same name is being used in two or more modules. (Is this what
the fuss is about with 'global variables'?)

Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)

Instead of having:

int GetNumber(void) { return 5; }

you have:

static int GetNumber(void) { return 5; }

The difference this makes is that when the compiler produces an object
file for the translation unit in question, it doesn't list "GetNumber"
in the object file's list of functions. Therefore, when the linker
links different object files together, it never sees the "GetNumber".
The net effect of this is:

1) You can have a function with the same name in a different
translation unit.
2) You cannot call the function from a different translation unit.

There is another kind of visiblity problem you might encounter. Let's
say you're writing a program that deals with Ethernet and also with
USB. For this, you might be using two different libraries, one for the
Ethernet and one for the USB. The Ethernet library might have a
function called "CheckConnection", and the USB library might have a
function by exactly the same name.

Since we want both these functions to be exported from their
respective object files, we have a problem.

In C++, the introduced the concept of "namespaces". Basically you'd
have:

USB::CheckConnection();
Ethernet::CheckConnection();

In C though, I think the best you can do is:

USB_CheckConnection();
Ethernet_CheckConnection();

That is to say, you'd have to actually change the function's name,
perhaps by prepending the library name to it.

Jun 27 '08 #9
On May 5, 5:55*pm, Tomás Ó hÉilidhe <t...@lavabit.comwrote:
You are right in thinking that the default is "extern", but
beware of the exception to the rule: if the object is const, then the
default becomes "static". I.e. the following two global object
definitions are the same:

* * int const i = 5;

* * static int const i = 5;

Sorry sorry sorry! Don't listen to me! I had a nagging in my head as I
was writing that that I might be confusing C with C++, so I opened up
notepad to try it out, and lo and behold I should have investigated my
nagging feeling before I posted.

The default for const global objects is in fact "extern" in C.
Jun 27 '08 #10
Bartc wrote:
I noticed that in C, functions in any module are automatically exported. So
that it's not possible to use the same function name in two modules (ie.
source files).
Look up the keyword "static". You will find that your assertion is false.
Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?
Look up the keyword "static".
More seriously, variables declared at file scope also seem to be
automatically exported. But in this case, the compiler/linker doesn't warn
me that the same name is being used in two or more modules. (Is this what
the fuss is about with 'global variables'?)
Look up the keyword "static".
Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)
Look up the keyword "static".
Jun 27 '08 #11

"Antoninus Twink" <no****@nospam.invalidwrote in message
news:sl*******************@nospam.invalid...
On 5 May 2008 at 11:12, Flash Gordon wrote:
>So what is wrong with static? Why suggest a non-standard extension when
a feature the language has had in all implementations for years is
sufficient.

Because they do different things. If you're making a shared library,
then you might want to have variables and functions that are globally
visible within your library, but don't pollute the namespace for users
of your library. Gcc's __attributes can achieve that.

Without extensions, you have only two choices: either make a variable
static, and so visible only within a single translation unit, or else
make it globally visible everywhere. Compiler extensions can provide
finer-grained visibility control, which seems to be what the OP had in
mind: when you link your shared library, all uses of a symbol throughout
the library files are resolved, but that symbol isn't put into the table
of externally-visible symbols in your .so file.
This is something that ought to be added to the language, I quite agree.
Very frequently you want library scope functions and variables.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jun 27 '08 #12
On May 5, 4:53 am, "Bartc" <b...@freeuk.comwrote:
I noticed that in C, functions in any module are automatically exported. So
that it's not possible to use the same function name in two modules (ie.
source files).

Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?
Declare the items you want to keep "private" static:

/** myfile.c */

/**
* The following variable and function cannot be
* referred to by name outside of myfile.c; however,
* they can still be referred to using pointers returned
* from other functions within the file.
*/
static int x;
static void foo() {...}
More seriously, variables declared at file scope also seem to be
automatically exported. But in this case, the compiler/linker doesn't warn
me that the same name is being used in two or more modules. (Is this what
the fuss is about with 'global variables'?)
The compiler will attempt to resolve multiple declarations of the same
variable into a single entity according to one of several models. The
key thing is that only one declaration can be a *defining*
declaration, and all the rest are *referencing* declarations. There
are several models by which the compiler determines which is a
defining vs. referencing declaration. Anything with an initializer is
considered a defining declaration; if you had two declarations with
initializers, and the initializers were different values, you would
get a diagnostic.

The problem with globals is that they promote tight coupling between
modules, discouraging code reuse. For example, if you had a sorting
routine that relied on the presence of a global variable to indicate
the array size, you could not reuse that routine in a program that did
not define that global.
Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)

-- Bartc
Like I said, use the static qualifier for each item you don't want
exported. Remember that doing so only prevents other modules from
referring to those entities by name; there's nothing preventing you
from writing a function to return a pointer to a static item, thus
making it available to other modules by reference. But it at least
allows you to reuse names.
Jun 27 '08 #13
On 5 May, 11:12, Antoninus Twink <nos...@nospam.invalidwrote:
On *5 May 2008 at *9:53, Bartc wrote:
I noticed that in C, functions in any module are automatically
exported. So
that it's not possible to use the same function name in two modules (ie. source files).
Now that I know that, I get work around it; but is there a way to
avoid the problem (short of lots of renaming)?

Check out your compiler's options. For example, gcc has quite a
sophisticated visibility model: in the simplest case, you can use
__attribute ((visibility("hidden"))) as a qualifier, or compile with
-fvisibility=hidden.
don't use compiler specific extensions when you don't have to.
Use static to keep things at file scope.
--
Nick Keighley
Jun 27 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: lawrence | last post by:
I get the impression that most people who are using objects in their PHP projects are mixing them with procedural code. The design, I think, is one where the procedural code is the client code, and...
5
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains...
7
by: John | last post by:
Hi, I'm looking for the best way to deal with globals in PHP. As a 'C' software developer, I would normally avoid all globals and not have any at all, but use structs and pass everything in...
3
by: Robert Dodier | last post by:
Hello, I'm interested in introducing new variables into the environment of a Python interpreter or program. In reading through old posts to this newsgroup, I see there is an often-repeating...
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
2
by: DFS | last post by:
Not sure whether it's bad programming practice or not, but I have a module of globals I declare in each system: Global ws As Workspace Global db As Database Global td As TableDef Global rs As...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
5
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg =...
1
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.