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 23, 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.
Discussions

gcc - C: undefined reference to `floor': Even though I am including the maths library with "-lm" - Stack Overflow
Also: I could not have found the ... had to know the answer :) ... Ye, with some configurations/versions the order didn't matter as much, but the correct way is that the libraries comes after the objects that reference them. There is this question which is an exact duplicate. ... Why am I getting “undefined reference ... More on stackoverflow.com
🌐 stackoverflow.com
February 15, 2006
gcc - C: Undefined reference to floor - Stack Overflow
I am using Eclipse on Ubuntu to write/compile/run C code. I am trying to build my project. Following is the output in the Eclipse console. 22:18:31 **** Build of configuration Debug for project Pr... More on stackoverflow.com
🌐 stackoverflow.com
Undefined reference - Raspberry Pi Forums
testuser@raspberrypi:/tmp $ gcc -o main1 main1.c /tmp/ccSfUqzn.o: In function `f': main1.c:(.text+0x14): undefined reference to `floor' collect2: error: ld returned 1 exit status testuser@raspberrypi:/tmp $ testuser@raspberrypi:/tmp $ gcc -o main1 main1.c -lm testuser@raspberrypi:/tmp $ PeterO More on raspberrypi.org
🌐 raspberrypi.org
January 26, 2022
math function failing
When I am trying to build the same in my PC I am getting lots of error over undefined reference of math function defined in math.h file. A sample as " :-1: error:-----FMI_Stdalone_Bool_multi_proj/Bool_Multi/sources/dsblock5.c:342: undefined reference to `floor'". More on forum.qt.io
🌐 forum.qt.io
17
0
August 21, 2022
🌐
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 - The math library is named as libm.so and is usually, located in /lib or /usr/lib in UNIX system like Ubuntu. Go to these locations and try to locate libm.so.
🌐
Bytes
bytes.com › home › forum › topic
undefined reference to floor() - Post.Byes
February 15, 2006 - Re: undefined reference to floor() That worked (i.e., -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.
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 ·
🌐
GitHub
github.com › mrjbq7 › ta-lib › issues › 57
Error trying to build ta-lib on Ubuntu 13.10 ("Undefined reference to sin, cos, floor, exp, etc.") · Issue #57 · TA-Lib/ta-lib-python
December 24, 2013 - gcc -g -O2 -o .libs/ta_regtest ...a-lib/src/.libs/libta_lib.so: undefined reference to `acos' /home/erb/tmp/ta-lib/src/.libs/libta_lib.so: undefined reference to `floorf' /home/erb/tmp/ta-lib/src/.libs/libta_lib.so: undefined reference to `sin' /home/erb/tmp/ta-lib/src/.lib...
Author   ErikBjare
🌐
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 17, 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.

🌐
Cprogramming
cboard.cprogramming.com › c-programming › 118174-undefined-reference-error.html
Undefined reference error
June 4, 2005 - 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 ·
🌐
SourceForge
sourceforge.net › home › browse › check › mailing lists
[Check-devel] undefined reference to `floor' | check
September 21, 2015 - You seem to have CSS turned off. Please don't fill out this field
🌐
GitHub
github.com › libcheck › check › issues › 156
Building example throws undefined references · Issue #156 · libcheck/check
October 12, 2022 - [ 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
🌐
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 Problems
There has been a change to the build tools in ubuntu 11.10 - The "--as-needed" flag is now passed to the linker. This primarily has implications for dynamic library linking but it also affects the order that libraries appear on the command line even for static linking. ... 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>
🌐
CopyProgramming
copyprogramming.com › howto › getting-undefined-reference-to-floor-on-running-make-in-pintos
Fixing "Undefined Reference to `floor`" Error in Pintos on Make - Undefined reference to `floor'
February 15, 2006 - Question: How do I fix undefined reference to floor in Pintos make? Replace LDFLAGS = -lm with LDLIBS = -lm in src/utils/Makefile, then run make clean && make. This ensures the math library links after objects, resolving the issue in GCC linkers.