指向 C 中内置函数的函数指针

Function pointer pointing to built in function in C

我正在尝试将函数指针设置为指向 pow 函数。 这是我的代码:

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

int main(void){
    double (*func)(double, double) = pow;
  return 0;
}

但程序无法编译。 我收到此错误:

$ gcc test.c -o test

/tmp/ccD6Pmmn.o: In function `main':
test.c:(.text+0x8): undefined reference to `pow'
collect2: error: ld returned 1 exit status

我正在使用 Ubuntu 15.10.

有人知道我的代码有什么问题吗?

谢谢

您需要通过命令行使用 -lm 进行编译或配置您的 IDE 以将其添加到链接过程中。这是因为有些库非常大,为了避免占用 space 你的程序和编译时间,这是在 C 语言开始时设置的,当时计算机速度慢得多,编译和编译会花费更多space 的问题至关重要。