473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

undefined reference to floor()

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

Feb 15 '06 #1
11 28022

Michael McGarry wrote:
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).
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Feb 15 '06 #2
"Michael McGarry" <mi*************@gmail.com> writes:
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?


<http://www.c-faq.com/fp/libm.html>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 15 '06 #3
That worked (i.e., -lm). Thanks!!!

Feb 15 '06 #4

Keith Thompson wrote:
"Michael McGarry" <mi*************@gmail.com> writes:
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?


<http://www.c-faq.com/fp/libm.html>


Do you know the history of the bug?

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Feb 15 '06 #5
"Nelu" <ta********@gmail.com> writes:
Keith Thompson wrote:
"Michael McGarry" <mi*************@gmail.com> writes:
> 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?


<http://www.c-faq.com/fp/libm.html>


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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 16 '06 #6
Michael McGarry wrote:
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."
Feb 16 '06 #7
[My spelling corrected]
Michael McGarry wrote:
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."

Feb 16 '06 #8
On Thu, 16 Feb 2006 00:25:21 GMT, Martin Ambuhl
<ma*****@earthlink.net> wrote in comp.lang.c:
Michael McGarry wrote:
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.


....but if you can't find the FAW, at least check the FAQ.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 16 '06 #9
>> Keith Thompson wrote:
<http://www.c-faq.com/fp/libm.html>
"Nelu" <ta********@gmail.com> writes:
Do you know the history of the bug?

In article <ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.org> wrote: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.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 16 '06 #10

"Nelu" <ta********@gmail.com> writes:
Michael McGarry wrote:
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).


At least on Solaris 8, the manual page for 'floor' refers to "-lm".
Feb 16 '06 #11

Arndt Jonasson wrote:
"Nelu" <ta********@gmail.com> writes:
Michael McGarry wrote:
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).


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).

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Feb 16 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
1
by: Codemutant | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** I just cannot find what is undefined in this code.
1
by: Dom | last post by:
I'm new to c++. Just started learning it 24 hours ago. Am running into a compile problem. Please, no one waste the effort telling me to google it. I've been researching it for quite a while with no...
1
by: Andre Janssen | last post by:
Hi.... I tried to compile the following src with this command: "g++ -Wall -o bla alsaswitch.cpp". The src is an example src of xosd package. #include <xosd.h> int main (int argc, char...
3
by: Michael Sgier | last post by:
Hi i get thousands of messages like below. How shall i resolve that? Thanks Mcihael Release/src/Utility/RawImage.o: In function `CMaskImage::CMaskImage(int, int, char const*)':...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
3
by: prakash.mirji | last post by:
Hello, I am getting below mention linker error when I tried to link my class test.C I use below command to compile test.C /usr/bin/g++ -g -fpic -fvisibility=default -D_POSIX_SOURCE...
2
by: zqiang320 | last post by:
Hello: I execute make ,then get error: $ make Making all in libsbml/src make: Entering directory `/home/internet/mydoc/test_pj/libsbml/src' ........ /bin/sh ./libtool --tag=CC --mode=link...
3
by: tvnaidu | last post by:
I compiled tinyxml files (.cpp files) and when I am linking to create final exe in Linux, I am getting lot of errors saying "undefiend reference" to "operator new" and "delete", any idea?. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.