我正在尝试设置一个函数指针以指向 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 以将其添加到链接过程中。这是因为有些库非常大,为了避免占用程序空间和编译时间,这是在 C 语言开始时设置的,当时计算机速度要慢得多,编译需要更多时间和空间问题至关重要。
关于c - 指向 C 中内置函数的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35964965/