undefined reference to `sqrt' [duplicate]

梦想的初衷 提交于 2019-12-10 23:37:25

问题


Part of my program is to calculate sqrt of float number. When I write sqrt(1.0f); I success to compile the program,but when I write sqrt(-1.0f); the compilation fails with undefined reference to 'sqrt' - I suppose that in this case the nan value will be returned... I compile the program uing gcc. When I compile it with visual studio it is compiled successfuly with negative argument to sqrt. How the problem could be solved Thank you


回答1:


You have to add the -lm flag on most Unix-based systems, as in:

Compile using:

gcc -c file.c

and then link using:

gcc -o program file.o -lm

Or if you don't want to separate the two compilation steps, simply write:

gcc -o program file.c -lm



回答2:


Link with -lm to link with the math library



来源:https://stackoverflow.com/questions/8780603/undefined-reference-to-sqrt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!