Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sqrtf() in C: "undefined reference to `sqrtf'"

I am using Linux, Ubuntu 12.04 (Precise Pangolin), and Geany for coding. The code I am writing in C worked completely fine until I used the sqrtf command to find the square root of a float.

Error: HAC3.c:(.text+0xfd7): undefined reference to `sqrtf' .

The part of code I am using sqrtf() in:

float syn(float *a, float *b, int dimensions)
{
    float similarity=0;
    float sumup=0;
    float sumdown=0;
    float as=0;
    float bs=0;
    int i;
    for(i=0; i<dimensions; i++)
    {
        sumup = sumup + a[i] * b[i];
        as = as + a[i] * a[i];
        bs = bs + b[i] * b[i];
    }
    sumdown = sqrtf(as) * sqrtf(bs);
    similarity = sumup / sumdown;
    return similarity;
}

I included math.h, but this doesn't seem to be the problem.

Is there a way to fix Geany so this won't come up again?

like image 399
captain monk Avatar asked Jun 07 '13 01:06

captain monk


1 Answers

In addition to the many fine answers here, the portable form of the command that supports C99 version of <math.h> is specified by POSIX as c99 -l m. That having been said, every important Linux compiler supports -lm.

like image 94
Davislor Avatar answered Sep 19 '22 06:09

Davislor