【发布时间】:2016-12-27 15:38:24
【问题描述】:
我在 GNU/Linux Debian 8.5 下编码
我有一个简单的程序。
如果我用gcc prog.c 编译它就可以了!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
int main(int argc, char const *argv[]) {
float _f = 3.1415f;
floor(_f);
ceil(_f);
return 0;
}
如果我添加pow(),它会说找不到pow,我需要添加gcc prog.c -lm 才能正确。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
int main(int argc, char const *argv[]) {
float _f = 3.1415f;
floor(_f);
ceil(_f);
pow(_f, 2);
return 0;
}
如果我是对的,pow()、ceil()、floor() 都来自<math.h>?
那么为什么floor() 和ceil() 不抛出编译错误,而pow() 不抛出-lm 标志呢?
【问题讨论】:
-
不要使用带有下划线前缀的标识符:stackoverflow.com/questions/38997919/…
-
@2501: 下划线后跟小写字母即可。
-
@Dani 仅用于局部变量。
-
@melpomene 是的,我在发布上述 cmets 后才意识到这一点。更好的重复:Need to use -lm in Clang with pow(), but not sqrt()、log(10.0) can compile but log(0.0) cannot?、Why is -lm not necessary in some cases when compiling and linking C code?
-
"...都来自
" 他们都不是。它们的实现在一个库中,通常在 libm内部的 IX'ish 系统上。math.h只是给出原型,所以编译器理解你的代码在“谈论”什么。链接器最终链接libm,正如-lm选项所告知的那样。
标签: c linker-errors flags undefined-reference math.h