-1

I'm trying to install PintOS on my local Ubuntu 14.04 machine. When I try to run make to compile the utilities. I get the following error.

ankitkal@ankitkal-Inspiron-5521:~/os/pintos/src/utils$ ls
backtrace  Makefile   pintos   pintos.~1.55.~  pintos-mkdisk             setitimer-helper.o  squish-unix.c
CVS        Makefile~  pintos~  pintos-gdb      setitimer-helper.c         squish-pty.c
ankitkal@ankitkal-Inspiron-5521:~/os/pintos/src/utils$ make
gcc -lm  setitimer-helper.o   -o setitimer-helper
setitimer-helper.o: In function `main':
setitimer-helper.c:(.text+0xbe): undefined reference to `floor'
collect2: error: ld returned 1 exit status
make: *** [setitimer-helper] Error 1
ankitkal@ankitkal-Inspiron-5521:~/os/pintos/src/utils$ 

The maths library (for the <math.h> header which is used in setitimer-helper.c) is not getting linked properly. When I look into the Makefile, this is the output.

ankitkal@ankitkal-Inspiron-5521:~/os/pintos/src/utils$ cat Makefile
all: setitimer-helper squish-pty squish-unix

CC = gcc
CFLAGS = -Wall -W
LDFLAGS = -lm
setitimer-helper: setitimer-helper.o
squish-pty: squish-pty.o
squish-unix: squish-unix.o

clean: 
    rm -f *.o setitimer-helper squish-pty squish-unix

Please tell me how to fix it. I'm using gcc-4.8.6 by the way.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ankit
  • 27
  • 1
  • 10
  • There are a number of questions of which this is a duplicate. The issue is listing libraries before object files — the answer is "don't". – Jonathan Leffler Sep 25 '15 at 04:00
  • But the question is how do you do that by changing the Makefile. – Ankit Sep 25 '15 at 04:37
  • OK; then there needs to be an edit to the question... Roughly along the lines of what I've just done. You can decide whether you prefer your mathematics library singular or plural. Also, don't forget to report the bug to the developers of Pintos; there are as many platforms where the makefile won't work as there are where it does work (or it works by accident — many versions of `make` put `${LDFLAGS}` and `${LDLIBS}` after the object files on the link line, which would have worked). Others don't — witness your problem. – Jonathan Leffler Sep 25 '15 at 04:59

1 Answers1

1
gcc -lm  setitimer-helper.o   -o setitimer-helper

The problem is in the order of your arguments to GCC. Try this:

gcc -o setitimer-helper setitimer-helper.o  -lm

This is because of the way that ld resolves undefined symbols when linking. Basically, the way you had it before, ld first sees -lm and says "I have no reason to include this library". It then includes your setitimer-helper.o which has an unresolved reference to floor. After that, there are no more libraries to consider, and floor remains unresolved.

If -lm comes afterward, it is able to resolve the reference to floor.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • What change do I need to make to the Makefile to do that? – Ankit Sep 25 '15 at 03:37
  • 1
    Changing LDFLAGS to LDLIBS in Makefile worked. It now runs gcc setitimer-helper.o -lm -o setitimer-helper – Ankit Sep 25 '15 at 03:39
  • Excellent. Glad I could help. Please remember to up-vote any answer that helped, and accept an answer if it solves your problem. – Jonathon Reinhart Sep 25 '15 at 03:42
  • It showed a couple of warnings but didn't give any errors. How do I modify the makefile so that -lm appears at the end? @Jonathon Reinhart – Ankit Sep 25 '15 at 04:19