Expert C Programming

(Jeff_L) #1

version and do the wrong thing. At one point, Sun's compiler group amended the compiler drivers so
that they coped with the situation. We changed the SunOS 4.x unbundled compiler drivers from SC0.0


through SC2.0.1 so they would "do the right thing" if a user omitted -lm. Although it was the right


thing, it was different from what AT&T did, and broke our compliance with the System V Interface
Definition; so the former behavior had to be reinstated. In any case, from SunOS 5.2 onwards a


dynamically linked version of the math library /usr/lib/libm.so is provided.


Handy Heuristic


Where to Put Library Options


Always put the -l library options at the rightmost end of your compilation command line.


Similar problems have been seen on PC's, where Borland compiler drivers tried to guess whether the
floating-point libraries needed to be linked in. Unfortunately, they sometimes guessed wrongly,
leading to the error:


scanf : floating point formats not linked


Abnormal program termination


They seem to guess wrongly when the program uses floating-point formats in scanf() or


printf() but doesn't call any other floating-point routines. The workaround is to give the linker


more of a clue, by declaring a function like this in a module that will be included in the link:


static void forcefloat(float *p)


{ float f = *p; forcefloat(&f); }


Don't actually call the function, merely ensure that it is linked in. This provides a solid enough clue to
the Borland PC linker that the floating-point library really is needed.


NB: a similar message, saying "floating point not loaded" is printed by the Microsoft C runtime
system when the software needs a numeric coprocessor but your computer doesn't have one installed.
You fix it by relinking the program, using the floating-point emulation library.


Watch Out for Interpositioning


Interpositioning (some people call it "interposing") is the practice of supplanting a library function by
a user-written function of the same name. This is a technique only for people who enjoy a good walk
on the wild side of the fast lane without a safety net. It enables a library function to be replaced in a

Free download pdf