[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Undefined reference?!

Posted on 2006-05-08
Medium Priority
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
  • 2
LVL 46

Accepted Solution

Kent Olsen earned 80 total points
ID: 16635638
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

ID: 16637039
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 !

Author Comment

ID: 16638333
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

ID: 16638434

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

834 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