[net.lang.c] Variables

sl@ukc.UUCP (S.Lange) (11/13/84)

 I am trying to write a set of functions and procedures in C which will be used 
as a set of library routines. But I can not find any way to make the variables
used inside these routines unavailable to the applications program.
The problem is hiding certain functions, procedures and variables from the
application program, but a few functions and procedures must be accessible
from the main program.
Ideally I would like to include the routines as a  #include .

               CAN ANYONE HELP PLEASE


                         THANX

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (11/14/84)

>  I am trying to write a set of functions and procedures in C which will be used 
> as a set of library routines. But I can not find any way to make the variables
> used inside these routines unavailable to the applications program.

This is what file "static" is for.  C provides only two levels of externs:
globally known ("extern", default) and private to source file ("static").
Unfortunately if you have several files in your library that you want to
share external data/functions without making them visible to user programs,
C has not provided any mechanism for that.  Two solutions are to bundle all
the related sources together in a single file (with the drawback that a
reference to any entry point will pull the whole object file into your
binary image), or to invent names for inter-file use that the user is not
likely to use in his code (e.g., _Q8Q_myfunc() ).

If you have "C with classes" or C++, that provides a superior information-
hiding mechanism.  However, you still have to contend with the two-level
global label scheme that the loader employs.

mjs@alice.UUCP (M. J. Shannon, Jr.) (11/14/84)

The way to do what you want is something like this:

/* file libstuff.h: */
extern int globlibfunc0();
extern double globlibvar0;

/* file libstuff.c: */
#include "libstuff.h"
double globlibvar0 = 3.1415926535;
static locallibvar0 = 17;
static int locallibfunc0();

static int
locallibfunc0(x, y, z)
double x, y, z;
{
	x /= y; z /= x;
	return (x < z);
}

int
globlibfunc0(x, y)
double x, y;
{
	return (locallibfunc0(x, y, locallibvar0));
}

/* file main.c: */
#include "libstuff.h"

main()
{
	double x, y;

	(void) scanf("%g,%g\n", &x, &y);
	printf("%d\n", globlibfunc0(x, y);
	exit(0);
}

The magic invloved is the keyword `static', which makes the identifier
local to the file in which it is declared (in which file it must also
be defined).  The header file should contain only aggregate declarations
(struct, union, enum, typedef, etc.) and function declarations, all of
which should be preceded by the keyword `extern' to indicate that they're
not defined here.

dat@hpcnoe.UUCP (dat) (11/21/84)

	Also, a standard mechanism to avoid collision of variables in
libraries with those in the calling program is to use leading under-
lines...so you could have the library structured like;

	static int	_myvar1, _myvar2;

	<routines>

and so on.  This is REALLY useful if your library has lots of
variables!

					Dave Taylor
-----------
"These opinions represent very little of worth, but they are worthwhile!"
						-- Dr. Littleton, Esq.