Symbols in library.

T

Torbak

I got some question about symbols in libraries ...

In libraries, there is public symbols and "not public" symbols
(private, static)... In C when we use the "static" keyword on the
declaration of a function, the function is not public in the library.

1- When I use a class, all is symbols are put in the public section of
the library. How can I change that. The keyword "private" in a class
is only for the langage or does it change (like "static") something in
libs ? Even in object file ?

2- Even symbols which are not "static" have there decorated name in
the library. (I use bindump to check that). How can I avoid that for
the private functions of my lib ?
What COFF is use for then ?

3- The keyword "static" is used to keep the use of something in the
file scope. If my lib is composed from many object file, how can I
"hide" private functions ?

Does somebody know how I can get a good documentation about library
files ?
Thanks
 
V

Victor Bazarov

Torbak said:
I got some question about symbols in libraries ...

In libraries, there is public symbols and "not public" symbols
(private, static)... In C when we use the "static" keyword on the
declaration of a function, the function is not public in the library.

Actually, the term used in C++ is "external linkage" versus "internal
linkage". "Public" has a different meaning in C++.
1- When I use a class, all is symbols are put in the public section of
the library.

Are they? The C++ language specification says nothing about that.
How can I change that.

You need to ask in a newsgroup that deals with your particular
compiler because creation of libraries is not defined by C++ as a
language.
The keyword "private" in a class is only for the langage or does it
change (like "static") something in libs ? Even in object file ?

That is not defined by the language specification.
2- Even symbols which are not "static" have there decorated name in
the library. (I use bindump to check that). How can I avoid that for
the private functions of my lib ?

That is not defined either, however, some compilers will not decorate
functions that are specified 'extern "C"' or decorate them differently
if they are specified as 'extern "Pascal"' or something. Again, this
is not defined by the C++ language itself.
What COFF is use for then ?

There is no "COFF" in C++. Perhaps you need to ask your question in
a newsgroup where "COFF" is on topic.
3- The keyword "static" is used to keep the use of something in the
file scope.

Actually, for symbols declared 'static' in a namespace scope, it makes
them have internal linkage (those names are not visible from other
translation units).
If my lib is composed from many object file, how can I
"hide" private functions ?

C++ doesn't specify that, but the solution I've seen is to only expose
some base class with all public interface virtual and pure, and then
implement the functionality in derived classes. Access to the actual
functionality should be given through a pointer to the base class,
returned from some library function (factory function), and behind the
pointer to base should actually be an object of the derived class.
Does somebody know how I can get a good documentation about library
files ?

You should ask your compiler developer/manufacturer/vendor.

Victor
 
A

Artie Gold

Torbak said:
I got some question about symbols in libraries ...

[posting from c.l.c++]

Standard C++ is silent on libraries, except to say that the standard
library exists and what it contains. Anything past that is a
platform/implementation issue.
Does somebody know how I can get a good documentation about library
files ?

Since library implementation is platform specific, platform
documentation is the way to go. <OT>For general info, John Levine's
"Linkers and Loaders" would be a good place to start.</OT>

HTH,
--ag
 
A

Alf P. Steinbach

I got some question about symbols in libraries ...

In libraries, there is public symbols and "not public" symbols
(private, static)... In C when we use the "static" keyword on the
declaration of a function, the function is not public in the library.

1- When I use a class, all is symbols are put in the public section of
the library. How can I change that.

If you can forget about "libraries" and "object files", which are not
specified by the C++ standard, the answer to what you probably intend
to ask is to use


* An anonymous namespace.


Example:


namespace // anon
{
class C
{
...
};
}


The keyword "private" in a class is only for the langage or does it
change (like "static") something in libs ? Even in object file ?

That, including the possible existence of libraries and object files,
depends on the compiler and linker (if a linker exists).


2- Even symbols which are not "static" have there decorated name in
the library. (I use bindump to check that). How can I avoid that for
the private functions of my lib ?

A literal interpretation of that question is meaningless.

Perhaps you mean: "Even when a non-member function is declared as
'static' I get a name conflict with another compilation unit". In
that case your compiler is broken. However, the mere presence of
a name in some compiled form of the C++ source does not mean that
there will be a name conflict, e.g., it might be debug information.

But most probably what you meant is literally what you wrote, which
means some confusion about the effect of the word 'static'.


What COFF is use for then ?

<ot>
COFF is an object file format. Use Google to find more info.
</ot>


3- The keyword "static" is used to keep the use of something in the
file scope.

Nope. 'static' has many different meanings depending on context.
If a non-member function or variable or constant is declared 'static'
then usage of that entity is restricted to the _compilation unit_,
which might comprise many files.

If my lib is composed from many object file, how can I "hide" private
functions ?

Interpretation: "How can avoid name clashes for functions that are
only used within one compilation unit?".

Most people just use 'static', at least, I _believe_ that's what most
people do. Some strictly standard-conforming people use anonymous
namespaces. That's because 'static' is deprecated for this usage, so
in some far-fetched theoretical sense it might stop working in version
3247.8 of some non-commercial experimental compiler, far in the future.

Does somebody know how I can get a good documentation about library
files ?

Start a new thread here, or search for old threads, about
documentation of the standard library, if that's what you mean.

You might also check the FAQ.

If you actually mean 'library files' as in '.lib files', that's
compiler- and system-specific, so check your compiler and system
documentation.
 
T

Torbak

Thanks for thoses answers.

About :
"2- Even symbols which are not "static" have there decorated name in
the library. (I use bindump to check that). How can I avoid that for
the private functions of my lib ?", in fact I make a mistake. I
should say "Even symbols wich ARE 'static' have there ..."

I fact my question should be shortly discribe like this : Libraries
are files wich contain compiled code. Then to be linked, they contain
symboles description to discribe to the linker where it can find
functions which are accesible in the library. When I programme my
lib, I distingue 2 types of functions : those which will be "public",
means appear in header file, could be called from other compomnent of
the prog ... And those wich should not be called by others.

ex :
/* private stuff */
static char textTab[] = {"Hello", "By" };
static void DisplayText( int i ) {
sprintf("I say :%s/n", textTab[i&1] );
}

/* public stuff */
void Talk( void ) {// my public function
int i = rand();
DisplayText(i); // call private function
}
in mylib.h
extern "C" void Talk( void );

When I compile my stupid lib (I don't checked if what I wrote is
good), in release mode with no debug information etc ... I'll find
all symbols in the lib using dumpbin ... "textTab", "DisplayText" and
of course "Talk".

I expected to find only my public "Talk" function and not the other
symbols like "DisplayText(int)".

Because for me, the "DisplayText" function should be linked directly
in the library, and no any information about it should appear in the
release object. I wounder find the same thing than if the
"DisplayText" function was not exist like : void Talk( void ) {// my
is alone. No call to "private" functions.
sprintf("I say :%s/n", textTab[rand()&1] );
}
Of course, this exemple is simple because my private function is
called one time, and could be inlined.
So in general I wounder something like this :
0x000xxx afunctionDescriptor // adress of my private func.
0XOOOyyy Talk(void)
...
call afunctionDescriptor // call or jmp ...
ret



So if I use long description function name and many little functions,
my lib will be larger than if I use short function name with big
functions ?

I use microsoft C compiler.

PS : I tried namespaces but it doen't change anything.
 
K

Kamal R. Prasad

I got some question about symbols in libraries ...


1- When I use a class, all is symbols are put in the public section of
the library. How can I change that. The keyword "private" in a class
is only for the langage or does it change (like "static") something in
libs ? Even in object file ?

The private keyword refers to members (data or function) which can
only be accessed by members of that class- as per ANSI C++ standard.
2- Even symbols which are not "static" have there decorated name in
the library. (I use bindump to check that). How can I avoid that for
the private functions of my lib ?
What COFF is use for then ?

neither non-members within the library nor outside can access them
even if they see it , if they have been defined to be private. The
static keyword inside a class has a (slightly) different meaning. eg:-
class C {
public:
int value;
....
};

unless you define an instance of class C, you will not be anle to set
the value.
with static, you can.
class C {
public:
static int value;
...
};
class C::value = 1 is valid because there is no instance required.
Further, there is only one copy of the variable no matter how many
instances of the class C have been defined/malloc()'ed.

The ANSI C++ compiler makes no attempt to hide the field from
functions outside the scope of the C++ file where it is defined.
3- The keyword "static" is used to keep the use of something in the
file scope.
Oustide the class/struct, if the static keyword is used, yes that is
what it is supposed to do.
If my lib is composed from many object file, how can I
"hide" private functions ?

Depends on whether you want to prevent access or hide info about
symbol names.
As another poster stated, you may want to use the external/internal
linkage stuff for the latter.

regards
-kamal
 
D

Dave Thompson

Thanks for thoses answers.

About :
"2- Even symbols which are not "static" have there decorated name in
the library. (I use bindump to check that). How can I avoid that for
the private functions of my lib ?", in fact I make a mistake. I
should say "Even symbols wich ARE 'static' have there ..."

[later] I use microsoft C compiler.

I thought the MS tool was dumpbin, not bindump. I don't know about MS
object format in particular, but many object formats can record both
symbols which are "visible" to and can be linked from other modules,
usually called "global" or "external", and also symbols which are are
still generated but cannot be linked and do not conflict with other
such symbols having the same namem, "local" or "internal". In
traditional Unix 'nm', the letter for symbol type is uppercase for
global (T=text, D=data, B=bss) and lowercase for local (t,d,b).
I fact my question should be shortly discribe like this : Libraries
are files wich contain compiled code. Then to be linked, they contain
symboles description to discribe to the linker where it can find
functions which are accesible in the library. When I programme my
lib, I distingue 2 types of functions : those which will be "public",
means appear in header file, could be called from other compomnent of
the prog ... And those wich should not be called by others.
Noting that this use of 'public' and 'private' is different from,
though somewhat similar to, the same-named access modifiers for
members of a class (or struct) in C++. (And also Java.)
ex :
/* private stuff */
static char textTab[] = {"Hello", "By" };

That doesn't agree in type. You wanted char * text [].
And the opposite of Hello should be Bye or Good-bye.
static void DisplayText( int i ) {
sprintf("I say :%s/n", textTab[i&1] );

The first argument to sprintf must be a buffer (char array) of
sufficient size into which the formatted result is written. You
apparently want printf. And /n is not a newline, \n is.
}

/* public stuff */
void Talk( void ) {// my public function
int i = rand();
DisplayText(i); // call private function
}
in mylib.h
extern "C" void Talk( void );

When I compile my stupid lib (I don't checked if what I wrote is
good), in release mode with no debug information etc ... I'll find
all symbols in the lib using dumpbin ... "textTab", "DisplayText" and
of course "Talk".

I expected to find only my public "Talk" function and not the other
symbols like "DisplayText(int)".
See above; you need to find the doc or a newsgroup for your particular
compiler/implementation/platform to check that the DisplayText you see
is harmlessly local, and if there is a way to not generate it at all.
Because for me, the "DisplayText" function should be linked directly
in the library, and no any information about it should appear in the
release object. I wounder find the same thing than if the
"DisplayText" function was not exist like : void Talk( void ) {// my
is alone. No call to "private" functions.
sprintf("I say :%s/n", textTab[rand()&1] );
}
Of course, this exemple is simple because my private function is
called one time, and could be inlined.

A function definition earlier in the same (preprocessed) source file
certainly can be inlined, if the compiler implements it; the compiler
may require an option to do inlining (check you doc) and must have
limits where if a called function is too big or contains certain kinds
of complexity it won't be inlined, although I would expect your very
simple example not to exceed any reasonable limits.
So in general I wounder something like this :
0x000xxx afunctionDescriptor // adress of my private func.
0XOOOyyy Talk(void)
...
call afunctionDescriptor // call or jmp ...
ret



So if I use long description function name and many little functions,
my lib will be larger than if I use short function name with big
functions ?
If you use lots of little functions, they will almost certainly
generate more code, as well as (much) more object-file structuring
information, and more debugging symbols if you generate those, which
is rarely the default, than the same functionality in fewer functions.

If you use longer names for functions, it won't change the actual
code; it will probably make the object-file structuring information
slightly larger, and debugging symbols if any slightly larger.

Object-file structuring information won't (in any case I know of)
affect the size of the actual runtime code, and on many systems
debugging symbols (if they exist at all) are not actually loaded at
runtime unless you actually use the debugger.
PS : I tried namespaces but it doen't change anything.

That's surprising; symbols in namespaces (other than the global
namespace) need to be fixed somehow so that e.g. foo::x and bar::x do
not conflict; usually if not always this is done by "mangling" the
name. Possibly the tool you are using automatically demangles the
names before displaying them; check for an option to suppress that.

- David.Thompson1 at worldnet.att.net
 

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

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,352
Latest member
DianeKulik

Latest Threads

Top