Undefined reference to 'fmod'
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
Quoted 9 lines · James Colannino
Hey everyone. I included the
math.hheader, proceeded to usefmod(), 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 statusDoes 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
Quoted 9 lines · James Colannino
Hey everyone. I included the
math.hheader, proceeded to usefmod(), 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 statusDoes 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.
Signature · 1 line
Glynn Clements <[email protected]>
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 4 lines · 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
Signature · 5 lines
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
Quoted 4 lines · James Colannino
Quoted 2 lines · Glynn Clements
No,
fmod()is in libm, so you have to add -lm to the link command.
Quoted 8 lines
Quoted 2 lines
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.hin 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 thatmath.his 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
Hi,
It cant be not all processors have floating point unit's Therefor it cannot be in the main standard libary
Quoted 32 lines
-----Original Message-----
From: [email protected] [mailto:linux-c- [email protected]] On Behalf Of James Colannino
Sent: 11 June 2006 18:58
To: Linux C Programming List
Subject: Re: Undefined reference to 'fmod'Quoted 4 lines · 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.hin 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 thatmath.his 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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 5 lines · James Stevenson
Quoted 5 lines
Quoted 4 lines
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.
Quoted 8 lines
Quoted 5 lines
Interesting. Compiling with the argument -lm fixed my problem. That leads me to another question: why are the functions found in
math.hin 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 thatmath.his 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.
Signature · 1 line
Glynn Clements <[email protected]>