sqrtf未定义对c中“sqrtf”的引用

问题描述 投票:11回答:3

你好我使用linux 12.04和geany进行编码。我在C中编写的代码完全正常,直到我使用sqrtf命令查找浮点的平方根。错误:HAC3.c :(。text + 0xfd7):未定义对`sqrtf'的引用。

我正在使用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;
}

我包括math.h但这似乎不是问题。所以我想知道有没有办法修复geany所以这不会再出现?我知之甚少所以尽量解释一下。

c linux math.h geany
3个回答
11
投票

转到Build - > Set Build Commands然后在C commands下单击空标签,它将允许您指定一个新标签(将其命名为Link)。输入gcc -Wall -o "%e" "%f" -lm - -lm将告诉它将math库链接到您的应用程序。点击OK

然后单击Build并选择新创建的标签 - Link。这应该为你做。


9
投票

您需要与-lm链接以提供数学函数。


0
投票

除了这里的许多精美答案之外,支持caz版本的<math.h>的命令的可移植形式是specified by POSIX作为c99 -l m。话虽如此,每个重要的Linux编译器都支持-lm

© www.soinside.com 2019 - 2024. All rights reserved.