question related to static linking

J

junky_fellow

Guys,

I am using a library that has some global variables.

static const int libGlobal1;
static const int libGlobal2;

There are some functions in this library that use these global
variables and some functions that don't use these them.

My question is, if I link my application to some function that does
not use any of the global variable, will the global variables of this
library be a part of my data section. (consider a unix system that
uses elf as object file format).

Or the global variables will always become a part of my data section,
whenever I link with any function of the library.

What about the static variables defined inside a library function ?
 
J

jacob navia

(e-mail address removed) a écrit :
Guys,

I am using a library that has some global variables.

static const int libGlobal1;
static const int libGlobal2;

There are some functions in this library that use these global
variables and some functions that don't use these them.

My question is, if I link my application to some function that does
not use any of the global variable,

You can't use them anyway since they are static.

will the global variables of this
library be a part of my data section. (consider a unix system that
uses elf as object file format).

Yes, but they aren't visible form the outside.
Or the global variables will always become a part of my data section,
whenever I link with any function of the library.

Since they are static, they will be part of your data section ONLY
if you link-in the module that contains those variables.
What about the static variables defined inside a library function ?

The same.
 
J

junky_fellow

Since they are static, they will be part of your data section ONLY
if you link-in the module that contains those variables.

Thanks Jacob. The library is a single file libxyz.a.

During linking, I give the name of this library. So, will these global
variables be part of my 'data section' ?

What about the static variables declared inside a function ?
 
J

jacob navia

(e-mail address removed) a écrit :
Thanks Jacob. The library is a single file libxyz.a.

WITHIN that single file there are many MODULES. There are utilities
that tell you what modules are inside. The ".a" suffix under unix
means just "archive", i.e. many object files are grouped into
a single archive, like many text files can be grouped into a
single .zip file.
During linking, I give the name of this library. So, will these global
variables be part of my 'data section' ?

You have to ask the linker which modules it is linking-in. Many linkers
have command line options to produce MAP files, that tell you
exactly which libraries contributed which code into the final
executable.
What about the static variables declared inside a function ?

The same.
 
K

Keith Thompson

I am using a library that has some global variables.

static const int libGlobal1;
static const int libGlobal2;

There are some functions in this library that use these global
variables and some functions that don't use these them.

My question is, if I link my application to some function that does
not use any of the global variable, will the global variables of this
library be a part of my data section. (consider a unix system that
uses elf as object file format).

Or the global variables will always become a part of my data section,
whenever I link with any function of the library.

What about the static variables defined inside a library function ?

The C standard doesn't talk about "data sections" or object file
formats.

If you declare variables but never use them, or if their use can have
no visible effect on the behavior of the program, the implementation
is allowed to eliminate them. Whether a particular implementaton will
do so is up to that implementation; it's not really a language issue.

Why do you care? I'm not saying you shouldn't, but perhaps you're
asking the wrong question. What exactly are you trying to accomplish?
If you're concerned about the space occupied by two int variables, I
suggest that it's not worth worrying about.
 
J

junky_fellow

The C standard doesn't talk about "data sections" or object file
formats.

If you declare variables but never use them, or if their use can have
no visible effect on the behavior of the program, the implementation
is allowed to eliminate them.  Whether a particular implementaton will
do so is up to that implementation; it's not really a language issue.

Why do you care?  I'm not saying you shouldn't, but perhaps you're
asking the wrong question.  What exactly are you trying to accomplish?
If you're concerned about the space occupied by two int variables, I
suggest that it's not worth worrying about.

Thanks a lot Keith.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top