为什么我在包含数学的时候,会得到“未定义的对sqrt”错误的引用呢?h头?

时间:2021-06-22 22:48:25

I'm very new to C and I have this code:

我对C很陌生,我有这段代码:

#include <stdio.h>
#include <math.h>
int main(void)
{
  double x = 0.5;
  double result = sqrt(x);
  printf("The square root of %lf is %lf\n", x, result);
  return 0;
}

But when I compile this with:

但是当我用:

gcc test.c -o test

I get an error like this:

我得到这样一个错误:

/tmp/cc58XvyX.o: In function `main':
test.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status

Why does this happen? Is sqrt() not in the math.h header file? I get the same error with cosh and other trigonometric functions. Why?

这为什么会发生?是sqrt()而不是数学。h头文件?我用cosh和其他三角函数得到相同的误差。为什么?

6 个解决方案

#1


107  

The math library must be linked in when building the executable. How to do this varies by environment, but in Linux/Unix, just add -lm to the command:

在构建可执行文件时,必须链接数学库。如何在不同的环境中执行这些操作,但是在Linux/Unix中,只需将-lm添加到命令中:

gcc test.c -o test -lm

The math library is named libm.so, and the -l command option assumes a lib prefix and .a or .so suffix.

数学库叫做libm。所以,-l命令选项有一个lib前缀和一个。后缀。

#2


18  

You need to link the with the -lm linker option

您需要链接到-lm链接器选项。

You need to compile as

您需要编译为。

gcc test.c  -o test -lm

gcc (Not g++) historically would not by default include the mathematical functions while linking. It has also been separated from libc onto a separate library libm. To link with these functions you have to advise the linker to include the library -l linker option followed by the library name m thus -lm.

在历史上,gcc(不是g++)在连接时不会包含数学函数。它也被从libc分离到一个单独的图书馆libm。要链接这些函数,您必须建议链接器包含库-l链接器选项,然后是库名称m这样的-lm。

#3


7  

This is a likely a linker error. Add the -lm switch to specify that you want to link against the standard C math library (libm) which has the definition for those functions (the header just has the declaration for them - worth looking up the difference.)

这可能是链接错误。添加-lm开关以指定您想要链接到标准的C数学库(libm),该库具有这些函数的定义(header仅具有声明,值得查找差异)。

#4


4  

Because you didn't tell the linker about location of math library. Compile with gcc test.c -o test -lm

因为你没有告诉链接器关于数学库的位置。与gcc编译测试。c - o - lm测试

#5


3  

You must link the header file math.h with your code. You can do this by typing -lm after your command.

您必须链接头文件数学。h和代码。您可以在命令之后键入-lm。

#6


1  

Add header:

添加标题:

#include<math.h>

# include < math.h >

Note: use abs(), sometimes at the time of evaluation sqrt() can take negative values which leave to domain error.

注意:使用abs(),有时在评估sqrt()时可以取负值,从而导致域错误。

abs()- provides absolute values;

abs()——提供绝对值;

example, abs(-3) =3

例子,abs(3)= 3

Include -lm at the end of your command during compilation time:

在编译期间,在命令的末尾包含-lm:

gcc <filename.extension> -lm

gcc <文件名。扩展> - lm

#1


107  

The math library must be linked in when building the executable. How to do this varies by environment, but in Linux/Unix, just add -lm to the command:

在构建可执行文件时,必须链接数学库。如何在不同的环境中执行这些操作,但是在Linux/Unix中,只需将-lm添加到命令中:

gcc test.c -o test -lm

The math library is named libm.so, and the -l command option assumes a lib prefix and .a or .so suffix.

数学库叫做libm。所以,-l命令选项有一个lib前缀和一个。后缀。

#2


18  

You need to link the with the -lm linker option

您需要链接到-lm链接器选项。

You need to compile as

您需要编译为。

gcc test.c  -o test -lm

gcc (Not g++) historically would not by default include the mathematical functions while linking. It has also been separated from libc onto a separate library libm. To link with these functions you have to advise the linker to include the library -l linker option followed by the library name m thus -lm.

在历史上,gcc(不是g++)在连接时不会包含数学函数。它也被从libc分离到一个单独的图书馆libm。要链接这些函数,您必须建议链接器包含库-l链接器选项,然后是库名称m这样的-lm。

#3


7  

This is a likely a linker error. Add the -lm switch to specify that you want to link against the standard C math library (libm) which has the definition for those functions (the header just has the declaration for them - worth looking up the difference.)

这可能是链接错误。添加-lm开关以指定您想要链接到标准的C数学库(libm),该库具有这些函数的定义(header仅具有声明,值得查找差异)。

#4


4  

Because you didn't tell the linker about location of math library. Compile with gcc test.c -o test -lm

因为你没有告诉链接器关于数学库的位置。与gcc编译测试。c - o - lm测试

#5


3  

You must link the header file math.h with your code. You can do this by typing -lm after your command.

您必须链接头文件数学。h和代码。您可以在命令之后键入-lm。

#6


1  

Add header:

添加标题:

#include<math.h>

# include < math.h >

Note: use abs(), sometimes at the time of evaluation sqrt() can take negative values which leave to domain error.

注意:使用abs(),有时在评估sqrt()时可以取负值,从而导致域错误。

abs()- provides absolute values;

abs()——提供绝对值;

example, abs(-3) =3

例子,abs(3)= 3

Include -lm at the end of your command during compilation time:

在编译期间,在命令的末尾包含-lm:

gcc <filename.extension> -lm

gcc <文件名。扩展> - lm