C 某些未定义的引用<;数学h>;功能-Ubuntu 20.04

C 某些未定义的引用<;数学h>;功能-Ubuntu 20.04,c,function,compiler-errors,math.h,C,Function,Compiler Errors,Math.h,今天我的笔记本重置了,重新安装了Ubuntu,出现了这个奇怪的问题。。。 每次我尝试运行pow、sin、cos和tan时,终端都会给我以下警告: /c/"testmath /usr/bin/ld: /tmp/ccauzwsW.o: in function `main': testmath.c:(.text+0x34): undefined reference to `pow' /usr/bin/ld: testmath.c:(.text+0x47): undefined referen

今天我的笔记本重置了,重新安装了Ubuntu,出现了这个奇怪的问题。。。 每次我尝试运行pow、sin、cos和tan时,终端都会给我以下警告:

/c/"testmath
/usr/bin/ld: /tmp/ccauzwsW.o: in function `main':
testmath.c:(.text+0x34): undefined reference to `pow'
/usr/bin/ld: testmath.c:(.text+0x47): undefined reference to `sin'
/usr/bin/ld: testmath.c:(.text+0x5a): undefined reference to `cos'
/usr/bin/ld: testmath.c:(.text+0x6d): undefined reference to `tan'
collect2: error: ld returned 1 exit status
我不知道是什么引起的。此外,当我直接在函数中插入一个值,如
y=pow(100,2)
时,它在终端中显示得非常完美,当我引用其他变量时,例如
sqrt(x)
时,问题确实发生了

以下是我用于测试的代码:

#include <stdio.h>
#include <math.h>

int main(void){
    float z ,x = 9.0, y, k, l, m ;
    y = sqrt(9.0);
    z = pow(x , 2);
    k = sin(x);
    m = cos(x);
    l = tan(x);

    printf("\nThis is the sqrt: %f\n", y);
    printf("\nThis is the pow: %f\n", z);
    printf("\nThis is the sin: %f\n", k);
    printf("\nThis is the cos: %f\n", m);
    printf("\nThis is the tan: %f\n", l);


    return 0;
}
#包括
#包括
内部主(空){
浮动z,x=9.0,y,k,l,m;
y=sqrt(9.0);
z=功率(x,2);
k=sin(x);
m=cos(x);
l=tan(x);
printf(“\n这是sqrt:%f\n”,y);
printf(“\n这是电源:%f\n”,z);
printf(“\n这是sin:%f\n”,k);
printf(“\n这是cos:%f\n”,m);
printf(“\n这是tan:%f\n”,l);
返回0;
}

如果有帮助的话,我目前正在运行Ubuntu 20.04 x64和带有C/C++扩展名的VSCode。

在您创建的所有对象文件之后放置
-lm
。如果包含数学库,则可能在对象文件之前包含它,这导致了您看到的问题。@WhozCraig我在代码中使用了cntrl+a cntrl+c cntrl+v:(.你能解释一下这个构建行是什么吗?你需要将数学库添加到你的程序/项目使用的库列表中。但是,你如何做到这一点需要了解Visual Studio代码。你描述的键序列在你使用的IDE之外没有任何意义。