Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C';s pow()不';不能使用可变指数_C_Pow - Fatal编程技术网

C';s pow()不';不能使用可变指数

C';s pow()不';不能使用可变指数,c,pow,C,Pow,我有一点代码: #include <math.h> int main(void){ pow(2.0,7.0); //Works double x = 3.0; pow(2.0,x); //Fails with error "undefined reference to 'pow'" return 0; } #包括 内部主(空){ 战俘(2.0,7.0); //工作 双x=3.0; 功率(2.0,x); //失败,错误为“未定义对“

我有一点代码:

#include <math.h>

int main(void){
    pow(2.0,7.0);
    //Works

    double x = 3.0;
    pow(2.0,x);
    //Fails with error "undefined reference to 'pow'"
    return 0;
}
#包括
内部主(空){
战俘(2.0,7.0);
//工作
双x=3.0;
功率(2.0,x);
//失败,错误为“未定义对“pow”的引用”
返回0;
}

我在Eclipse编译器设置中链接了
-lm
gcc-O0-g3-Wall-lm-c-fmessage length=0-MMD-MP-MF“src/pgm.d”-MT“src/pgm.d”-o“src/pgm.o”“../src/pgm.c”
,因此我不确定错误的来源。我到底在做什么?

-lm
放在命令行的末尾。

您的
-lm
选项不起作用,因为命令行上的输入源:

在命令中写入此选项的位置会有所不同;链接器按照指定的顺序搜索和处理库和对象文件。因此,
foo.o-lz bar.o
在文件
foo.o
之后但在
bar.o
之前搜索库
z
。如果
bar.o
引用了
z
中的函数,则可能无法加载这些函数


第一个
pow(2.0,7.0)
有效,因为编译器将其作为常量表达式进行计算,并且在运行时不需要
pow

您需要使用编译器的-lm标志链接到数学库


第一个示例之所以有效,是因为编译器可以内联值(事实上,2^7始终等于128),但当使用变量参数to pow()时,它无法内联其值,因为它的值只有在运行时才知道,因此您需要显式链接标准数学库,而不是内联值,它将调用该函数。

我不确定在Eclipse中如何执行该操作;我能做的最好的事情就是
gcc-O0-g3-Wall-c-fmessage length=0-lm-MMD-MP-MF“src/PGM.d”-MT“src/PGM.d”-o“src/PGM.o”“../src/PGM.c”
,这并不能解决问题。在
Build->Settings->Linker->Libraries
帮助中添加
m
是否可能是同样的问题:,请参阅最后一个答案我可能的作品副本: