undefined reference to floor()

M

Michael McGarry

Hi,

I am compiling a C source file that includes math.h but I get an error
message that states undefined reference to floor().

How could that happen if this function is in math.h, isn't in the
standard library? Do I need to link it explicitly?

Thanks,

Michael
 
N

Nelu

Michael said:
Hi,

I am compiling a C source file that includes math.h but I get an error
message that states undefined reference to floor().

How could that happen if this function is in math.h, isn't in the
standard library? Do I need to link it explicitly?

On Solaris and Linux the math functions are in a different library that
the
compiler does not link against unless you tell it to. It's called
libm.so (-lm).
 
K

Keith Thompson

Nelu said:
Do you know the history of the bug?

It's arguable whether it's actually a bug.

The math library is fairly large, and a lot of C program don't use it
(there's seldom much need for floating-point math in system code).
Back when systems were smaller and linkers were dumber, there was a
significant advantage in not including the math code in the standard
library; it made a lot of programs substantially smaller.

These days, size is less of an issue (but still not insignificant),
and linkers *should* be able to load only the functions that are
actually called. (I'm actually not sure of the current state of the
art in linkers, and the existence of dynamic libraries probably
confuses things further.)
 
M

Martin Ambuhl

Michael said:
Hi,

I am compiling a C source file that includes math.h but I get an error
message that states undefined reference to floor().

How could that happen if this function is in math.h, isn't in the
standard library? Do I need to link it explicitly?

Learn to check the FAW before posting. Sometimes you need to be a tiny
bit creative to decide which question is appropriate. In your case, it is
<http://c-faq.com/fp/libm.html> "I'm trying to do some simple trig, and
I am #including <math.h>, but the linker keeps complaining that
functions like sin and cos are undefined."
 
M

Martin Ambuhl

[My spelling corrected]
Michael said:
Hi,

I am compiling a C source file that includes math.h but I get an error
message that states undefined reference to floor().

How could that happen if this function is in math.h, isn't in the
standard library? Do I need to link it explicitly?

Learn to check the FAQ before posting. Sometimes you need to be a tiny
bit creative to decide which question is appropriate. In your case, it is
<http://c-faq.com/fp/libm.html> "I'm trying to do some simple trig, and
I am #including <math.h>, but the linker keeps complaining that
functions like sin and cos are undefined."
 
C

Chris Torek

It's arguable whether it's actually a bug.
Indeed.

The math library is fairly large, and a lot of C program don't use it
(there's seldom much need for floating-point math in system code).
Back when systems were smaller and linkers were dumber, there was a
significant advantage in not including the math code in the standard
library; it made a lot of programs substantially smaller.

Although even by the mid-1980s this was not much of a reason.
These days, size is less of an issue (but still not insignificant),
and linkers *should* be able to load only the functions that are
actually called. (I'm actually not sure of the current state of the
art in linkers, and the existence of dynamic libraries probably
confuses things further.)

Dynamic libraries do make things different.

One of the important ways they make things different is that they
offer the opportunity to optimize the system at boot time.

Suppose, for instance, that you have a Celeron, or an Athlon, or
a Pentium4, or a PentiumPRO, or an x86-64, or any of the myriad
other implementations that everyone uses since nobody ever uses an
architecture other than the x86. :) There are ways to implement
the math functions so that they will work on every one of these
machines -- but no matter which way(s) you pick, they will be
unnecessarily slow on *some* of them, but very fast on some other(s).

Ideally, what you would like to have is a system in which the
math routines are chosen based on the CPU.

The CPU is, of course, fixed at boot time (you must power down the
machine to replace it). So, why not build 37 (or however many it
takes) *different* math libraries, each one optimal for one particular
implementation, and have the boot process choose the correct one?

To do this, of course, we have to have some code at boot time that
picks out the right library. This code will not do any math itself,
but -- if we write it in C -- will need the *rest* of the C library
(or some substantial portion thereof). So this works best if the
math library is a separate library, that can be installed during the
boot process by a program that uses the C library but not the math
library.

The actual benefit is pretty small on the x86 family, but can be
enormous on those other architectures that no one ever uses. In
particular, systems where floating-point hardware is optional will
often want to have a "software-only" math library and a "hardware-
based" math library.

(As it happens, various *other* functions, including those one would
want in the library used by the boot-time library-selection program,
are included in the set one might want to have swapped about. So
this can work even better if one can arrange to have multiple versions
of the entire C runtime libraries, math or not; but separating out
the libm section is easier and gets most of the performance in some
important cases.)
 
A

Arndt Jonasson

Nelu said:
On Solaris and Linux the math functions are in a different library that
the
compiler does not link against unless you tell it to. It's called
libm.so (-lm).

At least on Solaris 8, the manual page for 'floor' refers to "-lm".
 
N

Nelu

Arndt said:
At least on Solaris 8, the manual page for 'floor' refers to "-lm".

On Linux it says "Link with -lm". I liked the Solaris manual pages
better though, because
they were telling about the libraries you had to link against in the
synopsis:
cc [flag ...] file ... -lm [library ...]. This is not the case with
Linux (actually I think it's
GNU).
 
Joined
Sep 13, 2012
Messages
2
Reaction score
0
Sorry, I have the same problem, but even when I use -lm flag I receive this error. What should I do?
 
Joined
Sep 13, 2012
Messages
2
Reaction score
0
here is my Makefile:
all: setitimer-helper squish-pty squish-unix

CC = gcc
CFLAGS = -Wall -W
LDFLAGS = -lm
setitimer-helper: setitimer-helper.o
squish-pty: squish-pty.o
squish-unix: squish-unix.o

clean:
rm -f *.o setitimer-helper squish-pty squish-unix

And the error happens in "setitimer-helper" file, which "floor" function is used in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top