0

Whenever I include math.h to my c code I can't compile without the -lm option. I get this error message:

d.o: In function `refresh_position':
d.c:(.text+0x4df): undefined reference to `sqrt'
d.c:(.text+0x524): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

I can compile it with just typing -lm but my teacher says that if code doesn't work i will get 0 point from that homework. I want to know is this error occurs because of my code or because of my computer or c library. I have to be sure about it will run without any error on my teachers computer.

Baran
  • 131
  • 8
  • 1
    If your teacher uses an implementation with the math library separate from the rest of the standard library, she should know how to link it all together when needed. I think it's an error to give 0 points for homework that requires `pow()` or `sin()` if the teacher does not know how to use her tools (unless, of course, it's assumed to be programming for free-standing environments). – pmg Mar 26 '20 at 16:52

1 Answers1

1

Some implementations such as gcc do not link the math library (called libm.a on most *nix implementations) by default, which is why you need to include the -lm when building the code.

Your teacher should be aware of issues like this, and as long as your code is using sqrt and other math routines correctly (using the right type for the inputs and output), he or she should be able to build your code such that it will run.

John Bode
  • 119,563
  • 19
  • 122
  • 198