sqrt() function link error

霸气de小男生 提交于 2019-12-31 02:34:04

问题


The following code is throwing undefined symbol error on Linux.

$ cat rms.c
/* sqrt example */
#include <stdio.h>
#include <math.h>

int main ()
{
  double param, result;
  param = 1024.0;
  result = sqrt (param);
  printf ("sqrt(%lf) = %lf\n", param, result );
  return 0;
}


$ gcc rms.c
/tmp/ccaWecFP.o(.text+0x24): In function `main':
: undefined reference to `sqrt'
collect2: ld returned 1 exit status

If I replace argument to sqrt() with (double)16 then program is compiling and executing. Why is this throwing error in first case.


回答1:


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.




回答2:


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.



回答3:


You must link with libm

gcc rms.c -lm

If you want more explanation Linking with external libraries.

Good Luck ;)




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/15743330/sqrt-function-link-error

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