什么是'未定义的参考'pow''[复制]

时间:2022-05-07 05:30:20

This question already has an answer here:

这个问题在这里已有答案:

I have a codepad .

我有一个键盘。

On line 15 inside a for function

在第15行内的for函数

 for(i=2; i<=90; i+=2){
    int j=0+i;
    printf("%i\n",i);
    power=pow(inp,j);
    factor=factorial(i);
    if(i%4==0)fAns += power/factor;
    else fAns -= power/factor;
  }

the line power=pow(inp,j); I added j instead of just using i because it gave me the same error. undefined reference to 'pow'.

line power = pow(inp,j);我添加了j而不是仅使用i,因为它给了我同样的错误。未明确引用'pow'。

If I replace j with 2, then it works just fine but when I use j=i it wont work. Is there a problem with incrementing this line?

如果我用2代替j,那么它工作得很好但是当我使用j = i时它不会工作。递增此行是否有问题?

I want this to increment and not throw me an error.

我希望这个增加,而不是给我一个错误。

1 个解决方案

#1


22  

You need to link with the math library. With gcc, this would mean passing -lm during linking.

您需要链接数学库。使用gcc,这意味着在链接期间传递-lm。

The reason it doesn't complain when you use 2 as the exponent value is because the compiler is optimizing the pow call out.

当你使用2作为指数值时它没有抱怨的原因是因为编译器正在优化pow调用。

#1


22  

You need to link with the math library. With gcc, this would mean passing -lm during linking.

您需要链接数学库。使用gcc,这意味着在链接期间传递-lm。

The reason it doesn't complain when you use 2 as the exponent value is because the compiler is optimizing the pow call out.

当你使用2作为指数值时它没有抱怨的原因是因为编译器正在优化pow调用。