解决C语言编译undefined reference to ‘pow’问题

时间:2025-04-02 11:59:07

在编译C语言源码的时候遇到如下问题:

undefined reference to ‘pow’

该函数定义在C的#include <>头文件内,明明应用了缺报错了,原因是要在编译的时候加 -lm信息后就能正常运行:

示例如下: gcc -lm

测试time中的pow函数,自己写了一个demo进行测试:

#include<>

#include<>

#include<>

int main(){

        pow(2,2);

        return 0;

}

编译运行一切正常,这里正常的原因是因为pow内的函数是常数,如果其中一个参数是变量的话就会报异常。所以引用time包的时候一定要在编译的时候加-lm信息。

参考undefined reference to `pow'_suwenqiang2011的博客-****博客