Undefined reference?!

Posted on 2006-05-08
Last Modified: 2010-10-05

I'm having trouble using the ceil() and floor() functions from the math.h library, when I compile a simple program I get this:
/tmp/ccbjF9bu.o(.text+0x5cc): In function `main':
: undefined reference to `ceil'
/tmp/ccbjF9bu.o(.text+0x5ee): In function `main':
: undefined reference to `floor'
collect2: ld returned 1 exit status

The code is:

float f = 2.5;

if( ceil(f)==f || floor(f)==f )
   printf("not decimal!");

It works fine if I pass them the value (ceil(2.5)) instead of the variable, though :S

Thanks in advance :)
Question by:bass20
    LVL 45

    Accepted Solution

    Hi bass20,

    The function prototype should, indeed, be in math.h.  But that doesn't necessarily ensure that they can be found for linking.  :(

    Try adding "-lm" to the gcc command line.

    Good Luck!
    LVL 53

    Expert Comment

    Or try this :

      ceil((double) f)

    since you say ceil(2.5) works. Although it shouldn't make a difference.

    Just a note though : comparing two floats (doubles) using the == will not always give the result you expect !
    LVL 1

    Author Comment

    The -lm option worked, thanks :)

    Infinity: I didn't know that; in my case it worked, in what cases is it likely to fail?

    Thanks :)
    LVL 53

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now