0

I use Geany for C programming. When I try to build the file it gives me

error:undefined reference to sqrt.

Compile: gcc -Wall -c "%f"

Build: gcc -Wall -o "%e" "%f"

Execute: ./%e

I tried to add -lm to the build command but then it gives me

gcc error:no such file or directory.Compilation Failed.
glglgl
  • 89,107
  • 13
  • 149
  • 217
Marvin Micek
  • 103
  • 4

1 Answers1

3

The compiler and linker options with arguments must be separate, you can't compile them like you do.

Make them separate, like e.g.

gcc -Wall -o "%e" "%f" -lm
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • @MarvinMicek The option `-l` (small letter L) tells the linker to link with a library, in this case a library called `m` which is the math library. – Some programmer dude Mar 03 '15 at 12:06
  • In a case where math.h is not included,will this affect the compilation?Because now my `Build Command` is `gcc -Wall -o "%e" "%f" -lm.` – Marvin Micek Mar 03 '15 at 12:09
  • @MarvinMicek The header file `` includes the *declarations* of the math library functions, without them the compiler will not know these functions exist. – Some programmer dude Mar 03 '15 at 12:10
  • Shouldn't this be enabled by default? – Marvin Micek Mar 03 '15 at 12:11
  • @MarvinMicek Do you mean the inclusion of ``? Or the linking of the math library? It's because of historical reasons that the math functions are in a separate library, because handling of floating point values used to be very complex and CPU intensive, as well as taking space which was at a premium back when C was created. – Some programmer dude Mar 03 '15 at 12:29
  • I meant the linking of the math library. – Marvin Micek Mar 03 '15 at 12:34