0

I'm trying to learn enough c to satisfy my occasional need to write simple programs that answer specific questions I have. I've been following a tutorial and using Geany for ease of use. Instead, I can't seem to get the simplest program to run. Here is my source code:

#include <stdio.h>
#include <math.h>

int main(int argc, char **argv)
{
    int x, y;
    double c, sqr_c;

    for (x = 10; x <= 31; x++)
    {
        for (y = 10; y <= 31; y++)
        {
            c = 1000 * x * x + y * y;
            sqr_c = sqrt(c);
            printf ("%f\n", sqr_c);
        }
    }

    return 0;
}

It compiles fine (gcc -c) but when I try to build an executable, I get:

gcc  "concsqr.c" -Wall -o "concsqr"  (in directory: /home/chip)
/tmp/cccSmdZS.o: In function `main':
concsqr.c:(.text+0x4b): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
Compilation failed.

I read something about making sure the linker can locate the library where sqrt() is defined, but I do not know how to do that, and wouldn't it be in a standard location anyway? Why doesn't the linker already know where it is? It's a standard library for c.

1 Answers1

0

You must try to compile your program with the -lm flag as libm.so is the math library, and the -l flag adds a lib prefix and .a or .so suffix.

gcc concsqr.c -Wall -o concsqr -lm 
shauryachats
  • 9,975
  • 4
  • 35
  • 48