You need to compile with the link flag -lm, like this:

gcc fib.c -lm -o fibo

This will tell gcc to link your code against the math lib. Just be sure to put the flag after the objects you want to link.

Answer from Fred on Stack Overflow
🌐
Reddit
reddit.com › r/c_programming › compilation error; why can't gcc recognize the floor function here?
r/C_Programming on Reddit: Compilation error; why can't gcc recognize the floor function here?
August 25, 2021 -

I am using gcc 9.4.0. In my main.c file, I do #include<math.h>, and then I #include "Heap.h". The heap file contains source code which uses the floor function:

int z; // Just testing here
z = (int) floor(3.5);
printf("hello, %i", z);


int parentBlockNum;
parentBlockNum = (int) floor((childBlockNum - 1) / 2.0) ;
parentValue = heap[2 * parentBlockNum] ;
parentIndex = heap[2 * parentBlockNum + 1] ;

The z is there just to test the floor function is working fine. The compiler is not giving me any errors for the z line, but actually for the parentBlockNum line: undefined reference to \floor` collect2: error: ld returned 1 exit status`

How could it possibly have trouble with the parentBlockNum line, when it has no issue with z?

I'm pretty sure I ran this code on a machine with gcc 11.2 and this wasn't happening (I don't have access to that machine now).

Top answer
1 of 3
6
Have you tried gcc -lm option?
2 of 3
5
How could it possibly have trouble with the parentBlockNum line, when it has no issue with z? Because the compiler is cleverer than you give it credit for. z is set to floor(3.5), which the compiler knows is 3. So it just replaces the assignment with z = 3 and the call to floor vanishes. It can do that because floor is a standard library function and the standard specifies exactly what it does. So the compiler is allowed to replace the call with anything that is precisely equivalent. But it can't do that with parentBlockNum unless it knows during compilation what the value of childBlockNum is, which is not evident. However, it could replace the computation of childBlockNum with equivalent integer arithmetic (assuming childBlockNum is an int). That's a more complicated optimization, but some versions of gcc might be able to do it, with certain optimization settings. Evidently, with the version you are using and default optimization flags, it doesn't, so the library function is actually needed. By the way, library functions are not in header files. The only thing that is in the header file is the declaration of the functions, so that the compiler knows what the argument and result types are. The actual function is in a library which must be linked into your executable. But not all libraries need to be named on the command line. The functions in stdio.h, for example, are in a standard library which is automatically searched by the linker. In the Gcc implementation, math functions -- the ones declared in math.h -- are in libm, which must be named on the command line. That's what -lm does.
🌐
Bytes
bytes.com › home › forum › topic
undefined reference to floor() - C / C++
February 15, 2006 - Do I need to link it explicitly?[/color] <http://www.c-faq.com/fp/libm.html> -- Keith Thompson (The_Other_Keit h) kst-u@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. ... Re: undefined reference to floor() That worked (i.e., -lm).
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › c/c++
Undefined reference - Raspberry Pi Forums
main.c:(.text+0x4): undefined reference to `floor' Looks like a missing "-lm" on the compile command.
🌐
OpenGenus
iq.opengenus.org › undefined-reference-to-pow-floorf-and-floor
[FIXED] Undefined reference to 'pow', 'floorf' and 'floor'
April 2, 2023 - opengenus.c:(.text+0x2a): undefined reference to 'pow' opengenus.c:(.text+0x4a): undefined reference to 'floor' opengenus.c:(.text+0x6a): undefined reference to 'floorf'
🌐
Reddit
reddit.com › r/cpp_questions › i am getting undefined reference to `floor' despite having -lm and every other function from math works except floor
r/cpp_questions on Reddit: I am getting undefined reference to `floor' despite having -lm and every other function from math works EXCEPT floor
November 19, 2019 -

Hi,

I am trying to build a big codebase for some days now and it builds fine on Windows and MacOS, but on Linux (Debian Jessie and Ubuntu 20.04) it fails with undefined reference to floor.

Code base is huge, but below is a log anyway - if you scroll to the end you will see -lm is there and also that it will still fail for undefined reference to floor and also a message saying floor is a hidden symbol.

https://api.cirrus-ci.com/v1/task/6465458310217728/logs/build.log

Any ideas what could cause this kind of failure to find floor?

I also get undefined referente to rand reported. So floor and rand are apparently not being found when linking :/

Also, not sure if relevant, but if I build everything dynamic then I get no errors, the errors only happens when I static link everything.

🌐
GitHub
github.com › libcheck › check › issues › 156
Building example throws undefined references · Issue #156 · libcheck/check
May 18, 2018 - [ 16%] Building C object src/CMakeFiles/money.dir/money.c.o [ 33%] Linking C static library libmoney.a [ 33%] Built target money Scanning dependencies of target main [ 50%] Building C object src/CMakeFiles/main.dir/main.c.o [ 66%] Linking C executable main [ 66%] Built target main Scanning dependencies of target check_money [ 83%] Building C object tests/CMakeFiles/check_money.dir/check_money.c.o [100%] Linking C executable check_money /usr/local/lib/libcheck.a(check.c.o): In function `tcase_create': check-0.11.0/src/check.c:145: undefined reference to `floor' check-0.11.0/src/check.c:148: und
Published   May 18, 2018
Author   Himeshi
Find elsewhere
🌐
Arch Linux Forums
bbs.archlinux.org › viewtopic.php
ld finds no floor() function in glibc / GNU/Linux Discussion / Arch Linux Forums
October 23, 2004 - » ld finds no floor() function in glibc · Pages: 1 · arthur_mueller · Member · From: Germany, RLP, Frankenthal · Registered: 2004-03-07 · Posts: 18 · Hi, I'm currently trying to write a simple program in C for converting astronomy coordinates. But during the compile time ld allways returns: time.o(.text+0x963): In function `julian_date': /home/user/astro/time.c:269: undefined reference to `floorf' time.o(.text+0x980):/home/user/astro/time.c:269: undefined reference to `floor' collect2: ld returned 1 exit status ·
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 118174-undefined-reference-error.html
Undefined reference error
August 2, 2009 - fctr.c: In function ‘main’: fctr.c:17: warning: format ‘%ld’ expects type ‘long int’, but argument 2 has type ‘int’ /tmp/ccfw9Xlk.o: In function `num_zeroes': fctr.c:(.text+0x33): undefined reference to `pow' fctr.c:(.text+0x3e): undefined reference to `floor' fctr.c:(.text+0x76): undefined reference to `pow' /tmp/ccfw9Xlk.o: In function `main': fctr.c:(.text+0x100): undefined reference to `numzeroes' collect2: ld returned 1 exit status My code is ·
🌐
Francisfisher
francisfisher.me.uk › problem › 2011 › undefined-reference-to-ceil-or-pow-or-floor-or-in-ubuntu-11-10
undefined reference to `ceil’ (or pow or floor or …) in ubuntu 11.10 | Computer Stuff
frankster@frankie-laptop:~> cat testm.c #include <math.h> int main(int argc, char*argv[]){ double x; double return_val = ceil(x); return 0; } frankster@frankie-laptop:~> gcc -lm testm.c /tmp/ccNkQLo6.o: In function `main': testm.c:(.text+0x15): undefined reference to `ceil' collect2: ld returned 1 exit status> A quick fix solution could be to pass the "-Wl,--no-as-needed" option to gcc which turns off the option in ld.
🌐
SourceForge
sourceforge.net › home › browse › check › mailing lists
[Check-devel] undefined reference to `floor' | check
November 27, 2024 - You seem to have CSS turned off. Please don't fill out this field
🌐
FreeBSD
forums.freebsd.org › development › userland programming and scripting
problem with math.h | The FreeBSD Forums
October 12, 2022 - EXP(3) Linux Programmer's Manual EXP(3) NAME exp, expf, expl - base-e exponential function SYNOPSIS #include <math.h> double exp(double x); float expf(float x); long double expl(long double x); Link with -lm. FreeBSD asks explicit to link math : "cc -lm". ... Thank for the reply. It turns out that I needed to add some compiler flags which did the trick: -lstdc++ and -lm
🌐
Texas Instruments E2E
e2e.ti.com › support › processors-group › processors › f › processors-forum › 364743 › compile-error-as-undefined-reference-to-floor
Compile error as "undefined reference to 'floor' " - Processors forum - Processors - TI E2E support forums
August 27, 2014 - I'm trying to add an opencv application code into /dvsdk_2_00_00_22\codec_engine_2_23_01\examples\ti\sdo\ce\examples\apps\video_copy\dualcpu\evmDM6446. This code was compiled and tested in CCS in ARM side. It runs ok for function test.
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › gcc-link-error-looking-for-log-floor-function-656777
gcc link error (looking for log, floor function)
July 19, 2008 - I got an error trying to link object files. It is looking for different math function gcc a.o b.o c.o -o myapp undefined reference to `log' undefined