简体   繁体   English

编译c代码时需要使用额外的选项,其中包括<math.h>

[英]Need to use extra option while compiling c code which includes <math.h>

Whenever I include math.h to my c code I can't compile without the -lm option.每当我将 math.h 包含到我的 c 代码中时,如果没有 -lm 选项,我就无法编译。 I get this error message:我收到此错误消息:

d.o: In function `refresh_position':
d.c:(.text+0x4df): undefined reference to `sqrt'
d.c:(.text+0x524): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

I can compile it with just typing -lm but my teacher says that if code doesn't work i will get 0 point from that homework.我只需输入 -lm 即可编译它,但我的老师说如果代码不起作用,我将从该作业中获得 0 分。 I want to know is this error occurs because of my code or because of my computer or c library.我想知道这个错误是因为我的代码还是因为我的电脑或c库。 I have to be sure about it will run without any error on my teachers computer.我必须确保它会在我的老师计算机上运行而不会出现任何错误。

Some implementations such as gcc do not link the math library (called libm.a on most *nix implementations) by default, which is why you need to include the -lm when building the code.默认情况下,一些实现(例如 gcc)不链接数学库(在大多数 *nix 实现中称为libm.a ),这就是为什么在构建代码时需要包含-lm原因。

Your teacher should be aware of issues like this, and as long as your code is using sqrt and other math routines correctly (using the right type for the inputs and output), he or she should be able to build your code such that it will run.你的老师应该意识到这样的问题,只要你的代码正确地使用了sqrt和其他数学例程(使用正确的输入和输出类型),他或她应该能够构建你的代码,这样它就会跑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM