function scope when making your own libraries 
Author Message
 function scope when making your own libraries

First of all I'm not sure what issues concerning libraries are covered
under ANSI/ISO and which are system dependent.  I have a book which
covers ISO C, but it does not have anything about libraries in it.
The project is currently built exclusively under UNIX however there is
interest in porting the project to NT, so I would like to follow ISO
standards whenever possible.

The project I am working on, uses a build process that generates
multiple *.a libraries using the "ar" command.  Each of these
libraries is used in a number of executables.  As the number of people
working on a project expands I've been worrying about two types of
name collisions.  One type of name collision occurs between internal
library functions and the functions contained in a specific
executable.  The other is a collision between two libraries that are
being linked to the same executable.

Because of this I would like to know if there are any ISO standards on
the controlling the scope of functions used for libraries.  For
instance I know that if you declare a function static in scope, that
it will only be linked to other functions inside the file.  What I
would like to know is, can you set the scope of a function to be
library internal. Meaning that, if you have a set of files used to
create a library, And you had a function foo defined in one of those
files.  Can you set the scope of function foo such that it can be
linked in any function in the set of files, but once the libraries is
built not link to any function outside of the library.

--
===================================================================
 Steven Feil           | Gram-pa, back at the turn of the      .~.
 Programmer/Developer  | century, why did people use an        /V\

                       | allowed to see the source code?      (X_X)
====================================================================



Mon, 25 Nov 2002 03:00:00 GMT  
 function scope when making your own libraries

Quote:

> Because of this I would like to know if there are any ISO standards on
> the controlling the scope of functions used for libraries.  For
> instance I know that if you declare a function static in scope, that
> it will only be linked to other functions inside the file.  What I
> would like to know is, can you set the scope of a function to be
> library internal. Meaning that, if you have a set of files used to
> create a library, And you had a function foo defined in one of those
> files.  Can you set the scope of function foo such that it can be
> linked in any function in the set of files, but once the libraries is
> built not link to any function outside of the library.

no, unfortunately not.  C has no support for namespaces.  a common (i should
probably say *the* common) method of dealing with this is to prepend (or
maybe even suffix (postpend?)) a string onto a symbol.  for example, in the
GGI library, all function names are prepended by ggi (e.g. ggiInit(),
ggiOpen(), ggiExit()) and all macros are prepended by GGI (e.g. GGI_AUTO).

that said, there is a pretty keen Unix way of doing it.  compile all your
objects into an .o instead of an .a:

        ld -o libhax0r.o a.o b.o c.o d.o e.o f.o

mine gives me a warning, but i can safely ignore it :D

then internalise the symbols you want with objcopy:

        objcopy -L foo

(you can than 'ar cru libhax0r.a libhax0r.o' if you want).  when you link
against this, the symbol 'foo' will now be as if you had declared it static.

also, there is something in Unix called 'weak' symbols, but i'm not entirely
sure what those are.

i've set the followup-to to comp.unix.programmer, since this is off-topic
for this newsgroup.

--
             /"\                                m i k e    b u r r e l l

              X        AGAINST HTML MAIL
             / \



Mon, 25 Nov 2002 03:00:00 GMT  
 function scope when making your own libraries
Hi !

You are right, there is no explicit standard for the linking roles. I
think
that there is no namespace defined in ANSI-C, so the only thing that you
have
to do (and I'm sure if you are working on a bigger project you _must_
do)
is to implement some naming conventions. you have to use prefixes for
your
public functions.
you might use structures with functionpointer which can use to build a
namespace

typedef struct customer_s customer_f;

typedef struct
{
   char *name;
   int  number;
   ...
   customer_f *f;

Quote:
} customer_t;

struct customer_s
{
   void (*setName)(customer_t *self, char *name);
   char *(*getName)(customer_t *self);
   ....

Quote:
} customer_f;

anyFunctionCall(..)
{
   customer_t *customer = ....;
   customer->f->setName(customer, ....)

Quote:
}

this example should tell an idea.

Quote:

> First of all I'm not sure what issues concerning libraries are covered
> under ANSI/ISO and which are system dependent.  I have a book which
> covers ISO C, but it does not have anything about libraries in it.
> The project is currently built exclusively under UNIX however there is
> interest in porting the project to NT, so I would like to follow ISO
> standards whenever possible.

> The project I am working on, uses a build process that generates
> multiple *.a libraries using the "ar" command.  Each of these
> libraries is used in a number of executables.  As the number of people
> working on a project expands I've been worrying about two types of
> name collisions.  One type of name collision occurs between internal
> library functions and the functions contained in a specific
> executable.  The other is a collision between two libraries that are
> being linked to the same executable.

> Because of this I would like to know if there are any ISO standards on
> the controlling the scope of functions used for libraries.  For
> instance I know that if you declare a function static in scope, that
> it will only be linked to other functions inside the file.  What I
> would like to know is, can you set the scope of a function to be
> library internal. Meaning that, if you have a set of files used to
> create a library, And you had a function foo defined in one of those
> files.  Can you set the scope of function foo such that it can be
> linked in any function in the set of files, but once the libraries is
> built not link to any function outside of the library.

> --
> ===================================================================
>  Steven Feil           | Gram-pa, back at the turn of the      .~.
>  Programmer/Developer  | century, why did people use an        /V\

>                        | allowed to see the source code?      (X_X)
> ====================================================================

--
Greetings from Lake Constance, Germany




Mon, 25 Nov 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. problem making my own library

2. making own libraries

3. making own libraries

4. I'd like to use own DLL made from C functions in a VC program

5. Making a view own it's own DC in MFC

6. Block-Scope vs Function-Scope, Speed Difference?

7. How to create my own function library ?

8. making a function library?

9. C function library for making graphs??

10. C4251 - enhancing 3rd-party library with own library

11. Making a new function or simulating the making

12. making my own irc client

 

 
Powered by phpBB® Forum Software