problem with cc compiler
Robert J. Drabek
robert at arizona.edu
Fri Jul 28 07:30:13 AEST 1989
In article <175 at sunquest.UUCP>, ggg at sunquest.UUCP (Guy Greenwald) writes:
> In article <4935 at alvin.mcnc.org>, spl at mcnc.org (Steve Lamont) writes:
> > Suppose that I wish to implement my own math library function, say sin()
>
> Declare the routine name before it is invoked, then define the routine to be
> static. Here's an example:
>
> main()
> {
> double sin();
> x = sin(something);
> }
>
> static double sin(angle)
> double angle;
> {
> /* Steve's slick sine calculation */
> }
>
> It is the combination of the static definition and the declaration before use
> that avoids the problem with the duplicate name in the run-time library.
Sigh.
First, Guy specifies (by default) external linkage for the function and
then later declares it to be static. A good compiler should complain
about this disparity in storage classes. Simply placing "static" at the
beginning of the first declaration could solve that.
BUT since the original poster talked about large existing code, we would
want to be careful about the possibility of <math.h> already being
included. So, we still haven't solved it.
Most importantly, though, is what happens during linking? Try it in
various environments and you will find that sometimes it links with the
library sin() [VMS] and sometimes with your own [Unix cc]. The linker
probably doesn't care about your declarations. (Try Ada if you really
want that.)
Name space problems have been around since before computers, and we
simply carried them along to this new domain.
What is wrong with creating a name for your new sine function which has
a lower probability of conflict (letting you get on to more interesting
topics) when you don't want to replace the library function? If you
really want to replace calls to such "standard" functions with calls to
your own, I don't think you will find a universal method using the
compiler and linker proper. The preprocessor, though, could be used
with something like maybe
#define sin(x) my_sin(x)
--
Robert J. Drabek
Department of Computer Science
The University of Arizona
Tucson, AZ 85721
More information about the Comp.lang.c
mailing list