Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Undefined reference to floor

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 Project1 ****
make all 
Building file: ../project1.c
Invoking: GCC C Compiler
gcc -I/lib/i386-linux-gnu -O0 -g3 -Wall -c -fmessage-length=0 -pthread -lm -MMD -MP -MF"project1.d" -MT"project1.d" -o "project1.o" "../project1.c"
../project1.c: In function ‘main’:
../project1.c:146:6: warning: unused variable ‘this_thread_id’ [-Wunused-variable]
../project1.c: In function ‘_pre_init’:
../project1.c:126:1: warning: control reaches end of non-void function [-Wreturn-type]
Finished building: ../project1.c

Building file: ../scheduler.c
Invoking: GCC C Compiler
gcc -I/lib/i386-linux-gnu -O0 -g3 -Wall -c -fmessage-length=0 -pthread -lm -MMD -MP -MF"scheduler.d" -MT"scheduler.d" -o "scheduler.o" "../scheduler.c"
Finished building: ../scheduler.c

Building target: Project1
Invoking: GCC C Linker
gcc -L/lib/i386-linux-gnu -lm -pthread -o "Project1"  ./project1.o ./scheduler.o   
./project1.o: In function `advance_global_time':
/home/akshay/Cworkspace/Project1/Debug/../project1.c:50: undefined reference to `floor'
collect2: ld returned 1 exit status
make: *** [Project1] Error 1

Can anyone please help me figure out what the problem is and how to solve it?

like image 606
Akshay7589 Avatar asked Jan 14 '23 07:01

Akshay7589


1 Answers

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.

like image 154
Jonathan Leffler Avatar answered Jan 20 '23 14:01

Jonathan Leffler