问题
Hello i am using linux 12.04 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:
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. So i am wondering is there any way to fix geany so this won't come up again? I've got little knowledge so try to explain if possible.
回答1:
Go to Build
-> Set Build Commands
then under C commands
click on the empty label and it will let you specify a new label (name it Link
). Type in it gcc -Wall -o "%e" "%f" -lm
- where -lm
will tell it to link the math
library to your app. Click OK
.
Then click on Build
and select your newly created label - Link
. This should do it for you.
回答2:
You need to link with -lm
to provide the math functions.
回答3:
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
.
来源:https://stackoverflow.com/questions/16974669/sqrtf-undefined-reference-to-sqrtf-in-c