sqrt() function not working with variable arguments [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-27 09:17:18

You need to link with the math library (use a '-lm' on the command line). In the constant case, the compiler is probably being smart and precomputing sqrt(2.0) (so the code that is compiled is essentially 'b = 1.414...;')

You probably need to add -lm when you compile. When you take the square root of a constant, the compiler is optimizing the code by taking the square root while it compiles, so it doesn't use sqrt at all.

Use the command gcc -Wall -o "test2" "test2.c" -lm which will likely fix this.

This includes the math library in addition to the standard C runtime library. On most systems, the math library is historically a separate entity that needs to be explicitly requested.

In case of gcc you need to link the library.

gcc filename.c -lm .

However in case of g++ no need to link the library so this will work fine :

g++ filename.c -o filename Once compilation is successful.

To run simply enter ./filename in G++. and enter ./a.out in Gcc.

Compile with:

gcc -Wall -o test2 test2.c -lm

You need to link against the math library.

hirday

include math library using " " operator

#include " math.h "

compile program using -lm option for inherit math library suppose our program name is test.c the we compile as follow

gcc test.c -lm

gcc does not link the standard libraries by default. So you just need to do this if compiling via gcc:

gcc filename.c -lm .

However in case of g++ no need to link the library so this will work fine :

g++ filename.c -o filename

This works fine for me. I think there is some problem with ur math library. Try linking it again and see. Other wise code is completely perfect.

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