linux下C语言编程解决warning : incompatible implicit declaration of built-in function问题

本文介绍了如何解决C语言编程中未声明库函数导致的编译警告问题,并提供了查询所需头文件的方法。

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

         在C语言编程过程中,偶遇如下warning,虽然并不影响最终的编译结果,但是看着warning也很无语,毕竟强迫症。

       我们可以发现被警告没有声明的都是常用的C库函数(这也是它为什么还能编译成功的原因),这种情况,是因为我们没有写函数对应的头文件的原因。 那么如何查询某个函数对应的头文件呢?

       在linux下,我们可以方便的使用命令

       man  函数名    来查询该函数需要哪个头文件来预编译。

       如

       man strlen

能够得到:

此时,我们再去C源码中添加 

#include <string.h>,那么就不会对strlen报warning啦,其他函数类比参照此过程。退出这个查询界面可以使用  shift+: 输入q(quit的缩写),然后便可以退出此界面了。

test2.c: In function ‘execute_script’: test2.c:22:34: warning: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration] 22 | result = realloc(result, strlen(result) + strlen(buffer) + 1); | ^~~~~~ test2.c:22:34: warning: incompatible implicit declaration of built-in function ‘strlen’ test2.c:7:1: note: include ‘<string.h>’ or provide a declaration of ‘strlen’ 6 | #include <security/pam_appl.h> +++ |+#include <string.h> 7 | test2.c:23:9: warning: implicit declaration of function ‘strcat’ [-Wimplicit-function-declaration] 23 | strcat(result, buffer); | ^~~~~~ test2.c:23:9: warning: incompatible implicit declaration of built-in function ‘strcat’ test2.c:23:9: note: include ‘<string.h>’ or provide a declaration of ‘strcat’ test2.c: In function ‘pam_conversation’: test2.c:39:31: warning: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration] 39 | (*resp)[i].resp = strdup((char*)appdata); // 使用预设密码 | ^~~~~~ test2.c:39:31: warning: incompatible implicit declaration of built-in function ‘strdup’ test2.c: In function ‘show_x11_dialog’: test2.c:75:30: warning: incompatible implicit declaration of built-in function ‘strdup’ 75 | char* msg_copy = strdup(message); | ^~~~~~ test2.c:76:26: warning: implicit declaration of function ‘strtok’; did you mean ‘strtoq’? [-Wimplicit-function-declaration] 76 | char* line = strtok(msg_copy, "\n"); | ^~~~~~ | strtoq test2.c:76:26: warning: initialization of ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion] test2.c:81:53: warning: incompatible implicit declaration of built-in function ‘strlen’ 81 | MARGIN, y_pos, line, strlen(line)); | ^~~~~~ test2.c:81:53: note: include ‘<string.h>’ or provide a declaration of ‘strlen’ test2.c:82:22: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion] 82 | line = strtok(NULL, "\n"); | ^ /usr/bin/ld: 找不到 -lXutf8 collect2: error: ld returned 1 exit status
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值