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 OverflowYou 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.
Add -lm to your link options, since pow() and floor() are part of the math library:
gcc fib.c -o fibo -lm
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).
You need to link libraries after the object files.
You have:
gcc -L/lib/i386-linux-gnu -lm -pthread -o "Project1" ./project1.o ./scheduler.o
You need:
gcc -L/lib/i386-linux-gnu -pthread -o "Project1" ./project1.o ./scheduler.o -lm
There seems to have been a change in the way the linkers work — at some time, it was possible to specify shared libraries (such as the maths library) before the object files, and all would work. However, nowadays, if the shared library doesn't satisfy any symbols when it is scanned, it is omitted from the link process. Ensuring that the object files are listed before the libraries fixes this.
See also Undefined reference to 'pthread_create'; same problem, same solution. And I doubt if that is the only such question in SO.
You need to link against the mathematical library, i.e., add -lm at the end of the link line. No idea how to do that in Eclipse, sorry.
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.
I have included the math.h header file but ceil() and floor() functions are not working. But pow() function works. I have given the error below.
/usr/bin/ld: /tmp/ccnYHlT4.o: in function `main':
stuff.c:(.text+0x1f): undefined reference to `floor'
collect2: error: ld returned 1 exit status
How to fix this?