linux c之加入了加入原文件 math.h调用abs()函数出现implicit declaration of function错误

在Linux环境下,使用C语言编程时,尽管引入了`<math.h>`头文件,调用`abs()`函数仍出现隐式声明错误。为解决此问题,可以将`abs()`函数替换为`fabs()`,因为`fabs()`用于处理浮点数,而`abs()`适用于整型。在Linux上,建议使用`fabs()`来替代`abs()`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 今天在vim 写C语言的时候

 代码我已经导入了#include<math.h>

但是当我调用ads()函数的时候出现了下面错误

 

解决办法:

把abs函数改写成fabs函数就行,然后去网上找原因,发现fabs是求浮点数的,ads求整形的,以后在linux上就用fabs函数就可以了

 

a.c: In function ‘Queens’: a.c:20:9: error: ‘solutions’ undeclared (first use in this function) 20 | if (solutions) { | ^~~~~~~~~ a.c:20:9: note: each undeclared identifier is reported only once for each function it appears in a.c:21:29: error: ‘solution_count’ undeclared (first use in this function) 21 | for (int i = 0; i < solution_count; i++) free(solutions[i]); | ^~~~~~~~~~~~~~ a.c:21:50: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration] 21 | for (int i = 0; i < solution_count; i++) free(solutions[i]); | ^~~~ a.c:2:1: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’ 1 | #include <stdio.h> +++ |+#include <stdlib.h> 2 | a.c:21:50: warning: incompatible implicit declaration of built-in function ‘free’ [-Wbuiltin-declaration-mismatch] 21 | for (int i = 0; i < solution_count; i++) free(solutions[i]); | ^~~~ a.c:21:50: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’ a.c:22:9: warning: incompatible implicit declaration of built-in function ‘free’ [-Wbuiltin-declaration-mismatch] 22 | free(solutions); | ^~~~ a.c:22:9: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’ a.c:27:24: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration] 27 | int *queen = (int*)malloc(n * sizeof(int)); | ^~~~~~ a.c:27:24: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’ a.c:27:24: warning: incompatible implicit declaration of built-in function ‘malloc’ [-Wbuiltin-declaration-mismatch] a.c:27:24: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’ a.c:28:5: warning: implicit declaration of function ‘memset’ [-Wimplicit-function-declaration] 28 | memset(queen, -1, n * sizeof(int)); | ^~~~~~ a.c:2:1: note: include ‘<string.h>’ or provide a declaration of ‘memset’ 1 | #include <stdio.h> +++ |+#include <string.h> 2 | a.c:28:5: warning: incompatible implicit declaration of built-in function ‘memset’ [-Wbuiltin-declaration-mismatch] 28 | memset(queen, -1, n * sizeof(int)); | ^~~~~~ a.c:28:5: note: include ‘<string.h>’ or provide a declaration of ‘memset’ a.c: In function ‘backtrack’: a.c:35:13: warning: implicit declaration of function ‘memcpy’ [-Wimplicit-function-declaration] 35 | memcpy(sol, queen, n * sizeof(int)); | ^~~~~~ a.c:35:13: note: include ‘<string.h>’ or provide a declaration of ‘memcpy’ a.c:35:13: warning: incompatible implicit declaration of built-in function ‘memcpy’ [-Wbuiltin-declaration-mismatch] a.c:35:13: note: include ‘<string.h>’ or provide a declaration of ‘memcpy’ a.c:39:29: warning: implicit declaration of function ‘realloc’ [-Wimplicit-function-declaration] 39 | solutions = realloc(solutions, (solution_count + 10) * sizeof(int*)); | ^~~~~~~ a.c:39:29: note: include ‘<stdlib.h>’ or provide a declaration of ‘realloc’ a.c:39:29: warning: incompatible implicit declaration of built-in function ‘realloc’ [-Wbuiltin-declaration-mismatch] a.c:39:29: note: include ‘<stdlib.h>’ or provide a declaration of ‘realloc’ a.c:46:17: warning: implicit declaration of function ‘is_safe’ [-Wimplicit-function-declaration] 46 | if (is_safe(row, col, queen)) { | ^~~~~~~ a.c: In function ‘Queens’: a.c:55:5: warning: incompatible implicit declaration of built-in function ‘free’ [-Wbuiltin-declaration-mismatch] 55 | free(queen); | ^~~~ a.c:55:5: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’ a.c: In function ‘main’: a.c:10:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 10 | scanf("%d", &n); | ^~~~~~~~~~~~~~~编译错误
05-29
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码莎拉蒂 .

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值