sqrt() function link error

浪尽此生 提交于 2019-12-01 22:00:48

This is a linker error.

The linker is missing the implementation of sqrt(). It resides in the library libm.

Tell GCC to add it by applying the option -lm.

The implementation of sqrt() is available in the math library or libm.

You have to link your program to the math library, as:

gcc rms.c -lm

A natural question is, how am I supposed to know this? The answer is in the manpages. If I do "man sqrt", I see the following. Note that the linking instruction is provided in the synopsis.

SYNOPSIS
       #include <math.h>

       double sqrt(double x);

       Link with -lm.

You must link with libm

gcc rms.c -lm

If you want more explanation Linking with external libraries.

Good Luck ;)

As the other answers say, you need to pass -lm in order to link to the library containing the sqrt symbol.

The reason it works with a constant argument is because sqrt is allowed to be implemented as a builtin. When GCC sees a builtin function called with constant arguments, it can calculate the result at compile-time instead of emitting a call to the external function in the library.

The other answers here discuss the linking error; I'm going to add an answer about why the result is correct if you replace param with a constant.

GCC has an optimization where it replaces functions with builtin equivalents (such as sqrt) and constant arguments (such as 16.0) with the results of those calculations (such as 4.0).

This is a form of constant folding.

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