Why does the following bit of code work in C:
为什么下面的代码在C中有效:
int res = pow(2, 3);
printf("%d\n", res);
while this other doesn't?
而这另一个不?
int a = 2;
int b = 3;
int res = pow(a, b);
printf("%d\n", res);
Even if I try
即使我尝试
double a = 2;
double b = 3;
double res = pow(a, b);
printf("%f\n", res);
I get an
我得到了
undefined reference to `pow'
未明确引用`pow'
What am I doing wrong?
我究竟做错了什么?
4 个解决方案
#1
52
When it works, it's because the calculation was done by the compiler itself (and included in the binary as if you wrote it out)
当它工作时,这是因为计算是由编译器本身完成的(并且包含在二进制文件中,就像你写出来一样)
printf("8\n");
When it doesn't work, is because the pow
function is included in the math library and the math library isn't linked with your binary by default.
To get the math library to be linked, if your compiler is gcc, use
如果它不起作用,是因为pow函数包含在数学库中,并且默认情况下数学库没有与二进制文件链接。要使数学库链接,如果您的编译器是gcc,请使用
gcc ... -lm ...
With other compilers, should be the same :)
but read the documentation
与其他编译器,应该是相同的:)但阅读文档
#2
16
undefined reference to 'pow'
sounds like a linker error. You are not linking in the math library, even if you introduce the function pow
by including <math.h>
.
对'pow'的未定义引用听起来像链接器错误。您没有在数学库中链接,即使您通过包含
With gcc, use the -lm
command line parameter to link in the math lib.
使用gcc,使用-lm命令行参数在math lib中进行链接。
#3
2
Use like this
像这样使用
#include <math.h>
#include <stdio.h>
int main(void)
{
for(int i = 1; i < 5; i++)
printf("pow(3.2, %d) = %lf\n", i, pow(3.2, i));
return 0;
}
Output:
输出:
pow(3.2, 1) = 3.200000
pow(3.2,1)= 3.200000
#4
-5
undefined reference to `pow'
未明确引用`pow'
because power to any number must have an integer value as power
因为任何数字的功率必须具有整数值作为功率
pow(x,y)
where, x must be real and y must be a whole number
#1
52
When it works, it's because the calculation was done by the compiler itself (and included in the binary as if you wrote it out)
当它工作时,这是因为计算是由编译器本身完成的(并且包含在二进制文件中,就像你写出来一样)
printf("8\n");
When it doesn't work, is because the pow
function is included in the math library and the math library isn't linked with your binary by default.
To get the math library to be linked, if your compiler is gcc, use
如果它不起作用,是因为pow函数包含在数学库中,并且默认情况下数学库没有与二进制文件链接。要使数学库链接,如果您的编译器是gcc,请使用
gcc ... -lm ...
With other compilers, should be the same :)
but read the documentation
与其他编译器,应该是相同的:)但阅读文档
#2
16
undefined reference to 'pow'
sounds like a linker error. You are not linking in the math library, even if you introduce the function pow
by including <math.h>
.
对'pow'的未定义引用听起来像链接器错误。您没有在数学库中链接,即使您通过包含
With gcc, use the -lm
command line parameter to link in the math lib.
使用gcc,使用-lm命令行参数在math lib中进行链接。
#3
2
Use like this
像这样使用
#include <math.h>
#include <stdio.h>
int main(void)
{
for(int i = 1; i < 5; i++)
printf("pow(3.2, %d) = %lf\n", i, pow(3.2, i));
return 0;
}
Output:
输出:
pow(3.2, 1) = 3.200000
pow(3.2,1)= 3.200000
#4
-5
undefined reference to `pow'
未明确引用`pow'
because power to any number must have an integer value as power
因为任何数字的功率必须具有整数值作为功率
pow(x,y)
where, x must be real and y must be a whole number