Ben said:
This is in the C FAQ.
14.3: I'm trying to do some simple trig, and I am #including <math.h>,
but I keep getting "undefined: sin" compilation errors.
A: Make sure you're actually linking with the math library. For
instance, under Unix, you usually need to use the -lm option, at
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.
Open letter to the gcc developers:
Dear Sirs:
Subject:
Gcc should link with the math library by default.
Rationale:
In comp.lang.c we receive since many many years the
same question posed always by beginners:
Why is my "sin" not linking? (Or "cos" whatever)
Most compilers now include a default library set
that includes the C run time, to which the math
library belongs.
The math library can be left out if you disable
this, what the 0.000000001% of users writing new
replacement math libraries for gcc will appreciate.
Implementation:
When parsing command line arguments, gcc searches for
--no_math_library. If found, the math library will
be deleted from the default library list. An argument
of --no_default_libs (or similar) could be used
for self-contained programs that do not use
any external library.
The default library list has two members at least:
The C run time library
The math library.
Other libraries could be specified somehow by
the user to avoid remembering to include them,
but at least those two should belong to the
default setting.
Rationale:
Beginners do not grasp the need to add a library,
and many do not know at all what a library is.
It is better to leave learning this fact after you
have done your first program.
Dennis Ritchie proposed a # preprocessor directive
in his Plan9 system to force the inclusion of the
library when you include the header file.
Lcc-win32 implements this with
#pragma lib "Absolute path"
#pragma lib <standard lib path>
This improves the language since it is no longer
necessary to figure out which library is used
with which header file. They are documented in
the header file with those constructs.
But I disgress
This is easy to do. Just a small change in the
gcc parsing routine, and a flag that saves the
info when generating the linker command line,
that is decorated automatically with
"-lm"
BY DEFAULT
This will save thousands of questions from
the beginners. Yes, you are advanced and
you wrote a compiler, but some day you were a
plain beginner and didn't even know that
you had to write that, isn't it?
Just add it up and make your system more
beginner friendly.
jacob