Discussion:
Undefined reference to 'fmod'
James Colannino
2006-06-11 04:35:49 UTC
Permalink
Hey everyone. I included the math.h header, proceeded to use fmod(),
then got the following error:

/tmp/ccKQxpJi.o: In function `main':
04-03-calc.c:(.text+0x144): undefined reference to `fmod'
collect2: ld returned 1 exit status

Does anybody know why it won't link? fmod() is a part of the standard
library. This makes me very frustrated :( Thanks very much in advance.

James
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Steve Graegert
2006-06-11 07:17:46 UTC
Permalink
Post by James Colannino
Hey everyone. I included the math.h header, proceeded to use fmod(),
04-03-calc.c:(.text+0x144): undefined reference to `fmod'
collect2: ld returned 1 exit status
Does anybody know why it won't link? fmod() is a part of the standard
library.
Use the -lm switch to link against libm.a (the math library).

\Steve
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Glynn Clements
2006-06-11 07:18:30 UTC
Permalink
Post by James Colannino
Hey everyone. I included the math.h header, proceeded to use fmod(),
04-03-calc.c:(.text+0x144): undefined reference to `fmod'
collect2: ld returned 1 exit status
Does anybody know why it won't link? fmod() is a part of the standard
library.
No, fmod() is in libm, so you have to add -lm to the link command.

As a general rule, anything which uses <math.h> has to link against
libm.
--
Glynn Clements <***@gclements.plus.com>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
James Colannino
2006-06-11 17:58:18 UTC
Permalink
Post by Glynn Clements
No, fmod() is in libm, so you have to add -lm to the link command.
As a general rule, anything which uses <math.h> has to link against
libm.
Interesting. Compiling with the argument -lm fixed my problem. That
leads me to another question: why are the functions found in math.h in a
separate library? I would have thought that all functions found in the
standard C library would be in glibc (The K&R book I'm reading said that
math.h is a part of the standard library.)

James
--
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"Blessed is the man, who having nothing to say, abstains from giving
wordy evidence of the fact." --George Eliot
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Steve Graegert
2006-06-11 19:20:54 UTC
Permalink
Post by James Colannino
Post by Glynn Clements
No, fmod() is in libm, so you have to add -lm to the link command.
As a general rule, anything which uses <math.h> has to link against
libm.
Interesting. Compiling with the argument -lm fixed my problem. That
leads me to another question: why are the functions found in math.h in a
separate library? I would have thought that all functions found in the
standard C library would be in glibc (The K&R book I'm reading said that
math.h is a part of the standard library.)
The functions in libm allow to create portable programs with
well-defined and standard-compliant behavior, e.g. when numerical
errors occur, and provide the application with a means to control
their behavior in abnormal cases via the matherr callback.
Additionally, they almost never rely on the features specific to
processor architectures and are thus slower and sometimes slightly
less accurate than the functions from libc.

libc functions OTOH are written for speed and exploitation of
CPU-specific features; they do not call matherr, and are therefore
much faster and, due to the extended 80-bit precision with which the
Intel FPUs carry their calculations, sometimes more accurate.

\Steve
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
James Stevenson
2006-06-14 15:41:07 UTC
Permalink
Hi,

It cant be not all processors have floating point unit's Therefor it cannot
be in the main standard libary
-----Original Message-----
Sent: 11 June 2006 18:58
To: Linux C Programming List
Subject: Re: Undefined reference to 'fmod'
Post by Glynn Clements
No, fmod() is in libm, so you have to add -lm to the link command.
As a general rule, anything which uses <math.h> has to link against
libm.
Interesting. Compiling with the argument -lm fixed my problem. That
leads me to another question: why are the functions found in math.h in a
separate library? I would have thought that all functions found in the
standard C library would be in glibc (The K&R book I'm reading said that
math.h is a part of the standard library.)
James
--
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/
"Blessed is the man, who having nothing to say, abstains from giving
wordy evidence of the fact." --George Eliot
-
To unsubscribe from this list: send the line "unsubscribe linux-c-
programming" in
More majordomo info at http://vger.kernel.org/majordomo-info.html
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Glynn Clements
2006-06-15 00:20:17 UTC
Permalink
Post by James Stevenson
Post by James Colannino
Post by Glynn Clements
No, fmod() is in libm, so you have to add -lm to the link command.
As a general rule, anything which uses <math.h> has to link against
libm.
Interesting. Compiling with the argument -lm fixed my problem. That
leads me to another question: why are the functions found in math.h in a
separate library? I would have thought that all functions found in the
standard C library would be in glibc (The K&R book I'm reading said that
math.h is a part of the standard library.)
It cant be not all processors have floating point unit's Therefor it cannot
be in the main standard libary
There are some functions in libc which use floating-point. Also, FP is
part of the C language itself; on processors which don't support FP in
hardware, you just emulate it.

The reason for the use of a separate math library is more likely to be
so that you can have different versions for systems with and without
FP support. Implementing FP math functions using an FP emulation
library is likely to be more efficient that relying upon generic FP
emulation.

Generic FP emulation normally works by handling "illegal instruction"
exceptions, which occur if a CPU without FP support attempts to
execute a FP instruction. The exception handler then emulates the
instruction in software. Exception handling normally adds significant
overhead, so it's more efficient to replace FP instructions with calls
to functions in an FP emulation library.
--
Glynn Clements <***@gclements.plus.com>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Loading...