c - 当我尝试编译我的代码时,即使与 'undefined reference to pow()' 链接时,我也会收到错误提示 '-lm'

标签 c linker-errors undefined-reference

我正在我的程序中创建一个用户定义的函数,当我尝试使用 pow 函数时,编译器给我一个错误并说有一个 undefined reference到 pow()。我见过与此类似的问题,解决方案是在编译时添加 -lm。我已经这样做了,但它仍然有问题。它只发生在我的用户定义函数上,因为它在我将 pow 放入代码的 main 函数之前就起作用了。如果指数是 2 而不是 0.5,它也有效。谢谢你的帮助。

 #include<math.h>

 float standard_deviation (float variance) {

       float deviation = 0.0;
       deviation = pow(variance, 0.5);

       return deviation;
 } /* mean */


 float standard_error (float standard_deviation, int number_of_elements) {

     return standard_deviation / pow(number_of_elements, 0.5);

 } /* mean */

最佳答案

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

float standard_deviation (float variance)
{
  float deviation = 0.0;
  deviation = pow(variance, 0.5);
  return deviation;
} /* mean */

float standard_error (float standard_deviation, int number_of_elements)
{
  return standard_deviation / pow(number_of_elements, 0.5);
} /* mean */

int main(int argc, char *argv[])
{
        printf ("%f\n", standard_deviation (1.0));
        return 0;
}

像这样调用时编译没有任何问题:

gcc test10.c -o test10 -lm

如果您首先将 .c 编译为 .o 文件,则只需要在生成可执行文件的最后链接阶段使用 -lm。

关于c - 当我尝试编译我的代码时,即使与 'undefined reference to pow()' 链接时,我也会收到错误提示 '-lm',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36882891/

相关文章:

c++ - 由于在 Bjarne Stroustrup "Programming and Practices using c++"中找不到符号导致的链接错误

c++ - 在 Windows 上使用 g++ 对 glfw3 的 undefined reference

c - 当我尝试从 C 程序调用已编译的 NASM 函数时出现 undefined reference 错误

ios - 链接器错误 : ld: library not found for -lPods (Trying to build . xcworkspace)

c++ - undefined reference

c - 通过指针访问数组的大小

c - 如何在不知道它是在堆栈还是堆上分配的函数内释放 C 中的数组?

c - SCTP回显服务器代码: Byteorder warning messages

C - 使用 getchar 和 putchar 交换字符

c++ - Visual C++ 将 LNK2019 错误与预编译 header 链接起来