2

I have one file foo.c:

#include <math.h>

int main(void) {
   double x = 0.5;
   double y = floor(x);
   return 0;
}

I am trying to compile it with the following command:

$ gcc -lm foo.c
/tmp/cc9xiNp2.o: In function `main':
foo.c:(.text+0x24): undefined reference to `floor'
collect2: error: ld returned 1 exit status

The operating system I am using says:

$ cat /etc/debian_version
jessie/sid
$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4

Including -lm always worked for me before. I'm not sure how to diagnose the problem?

Adrian Smith
  • 17,236
  • 11
  • 71
  • 93
  • 6
    The `-lm` should be after the `.c`. – a3f Aug 01 '16 at 13:50
  • 1
    I see, that works great, thanks. This was not the case on other versions of gcc I have used. Also: I could not have found the question that my question was a "duplicate" of; in order to search for that question I would have already had to know the answer :) – Adrian Smith Aug 01 '16 at 13:54
  • 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](http://stackoverflow.com/questions/16344445/undefined-reference-to-pow-even-with-math-h-and-the-library-link-lm). – a3f Aug 01 '16 at 14:09

0 Answers0