gcc 引用math.h头文件,编译出现undefined reference to `pow‘等错误时,需要加参数lm.

在自己编写的函数中调用数学函数时,如下例子:

#include<stdio.h>
#include<math.h>
void p(void)
{
    printf("%g\n", pow(2, 3));
}
        
int main()
{
    p();
    return 0;
}

出现编译问题:

undefined reference to `pow'

解决方法:

gcc pow.c -lm

问题:

为什么在函数中调用math.h头文件需要加 -lm参数,而在main中调用却不需要? 

原文地址:https://www.cnblogs.com/youngkingwang/p/2972917.html