Linux下vscode C语言 对pow、exp未定义引用问题

问题描述

头文件使用math库时会出现“未定义引用”问题
报错:

/tmp/cct7CPFw.o:在函数‘main’中:
19012705.c:(.text+0x88):对‘pow’未定义的引用
collect2: error: ld returned 1 exit status

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <math.h>
#define e 2.718281828
int main()
{
float temperature, dewpoint, humidex;
while (scanf("T %f D %f", &temperature, &dewpoint) == 2)
{
getchar();
humidex = temperature + (0.5555) * (-10 + 6.11 * pow(e, (5417.7530 * ((1 / 273.16) - (1 / (dewpoint + 273.16))))));
printf("T %.1f D %.1f H %.1f\n", temperature, dewpoint, humidex);
}
return 0;
}

在linux终端下也会出现一样问题
在这里插入图片描述

原因

linux中gcc没有默认链接math库

解决方法

vscode工作区文件夹下有一个.vscode文件夹(没有就创建),在该文件下打开settings.json(没有就创建)
左侧的4复制到右侧
“C”如图加上-lm,保存即可

结果

)`

补充

linux上c语言的函数库在/usr/lib/目录下,以lib*开头.so为后缀,*为库的名称,比如math库是libm.so,stdio库是libc.so
gcc命令末尾-l库名称就能链接函数库

…………………………………………………………………………………………………………..

补充

以上内容其实是对code runner插件问题的解决,在调试中仍然会出现问题,这时需要对./.vscode/tasks.json文件进行修改
20190218005159558