warning: incompatible implicit declaration of built-in function ‘strlen’

本文探讨了一个关于C语言程序中出现的编译警告:内置函数‘printf’的不兼容隐式声明问题。通过给出具体的源代码示例,分析了在不同操作系统环境下编译器对头文件处理的区别。

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

warning: incompatible implicit declaration of built-in function ‘printf’  出现这样的警告一般是缺少头文件或者头文件不正确。

我的程序用到了函数strlen,以前在redhat9.0下编译可以通过,为什么在fedora8下面却出现错误?
源代码如下:

C/C++ code
#include <stdio.h> #include <sys/socket.h> #include <netinet/in.h>
#include <string.h> int main() { int s; char dgram[512]; char dd[512] = "hello client!"; struct sockaddr_in adr_ser; struct sockaddr_in adr_cli; adr_ser.sin_family = AF_INET; adr_ser.sin_port = htons(6666); adr_ser.sin_addr.s_addr = inet_addr("192.168.1.137"); s = socket(AF_INET,SOCK_DGRAM,0); bind(s,(struct sockaddr*)&adr_ser,sizeof(adr_ser)); int len = sizeof(adr_cli); recvfrom(s,dgram,sizeof(dgram),0,(struct sockaddr*)&adr_cli,&len); printf("From client:%s\n",dgram); sendto(s,"Hello",strlen("Hello")+1,0,(struct sockaddr*)&adr_cli,len); close(s); return 0; }


test2.c: In function ‘execute_script’: test2.c:22:34: warning: implicit declaration of functionstrlen’ [-Wimplicit-function-declaration] 22 | result = realloc(result, strlen(result) + strlen(buffer) + 1); | ^~~~~~ test2.c:22:34: warning: incompatible implicit declaration of built-in functionstrlen’ test2.c:7:1: note: include ‘<string.h>’ or provide a declaration ofstrlen’ 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 functionstrlen’ 81 | MARGIN, y_pos, line, strlen(line)); | ^~~~~~ test2.c:81:53: note: include ‘<string.h>’ or provide a declaration ofstrlen’ 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、付费专栏及课程。

余额充值