gpt4 book ai didi

c - 为什么我会收到 "undefined reference to sqrt"错误,即使我包含了 math.h header ?

转载 作者:太空狗 更新时间:2023-10-29 16:14:57 29 4
gpt4 key购买 nike

<分区>

我是 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;
}

但是当我编译它时:

gcc test.c -o test

我收到这样的错误:

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

为什么会这样? sqrt() 不是在math.h 头文件中吗?我在 cosh 和其他三角函数中遇到了同样的错误。为什么?

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com