gpt4 book ai didi

c - 编译时错误 "undefined reference to "

转载 作者:行者123 更新时间:2023-11-30 20:56:49 25 4
gpt4 key购买 nike

当我编译这个文件时,它不断地给我

Undefined reference to size
Undefined reference to file

我在第二次编译它两次时都给出了错误,而第一次时它只给出了第二个错误。

我能确切地知道问题是什么以及如何解决它吗?

ps:我使用gcc -o ser server.c来编译

抱歉,我的网络出现问题

 #include<stdio.h>
#include<errno.h>
#include<arpa/inet.h>
#include<string.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<fcntl.h>

#define Max_len 1049
void error (char * str)
{
perror(str);
exit(0);
}

这是完整的代码

int main (int argc, char ** arg)
{
//------------------------- define data type
int listenfd,connfd,clilen,portno,fil_fd;
char buff[Max_len], *str,f_name[100],car=0xAA;
struct sockaddr_in servaddr,cliaddr;
FILE * file_ptr; //to store the pointer that point to the read file
//------------------------- socket intinalizing
if(argc != 2) //of user forget to insert the port number
error("missing port number to complet establishment\n");

portno=atoi(arg[1]); //convert the port to int and check if it was within the rang
if((portno<=22000) || (portno>=23000))
error("your port number not valid .... pleas insert one in reng (22000,22999)\n");

listenfd=socket(AF_INET,SOCK_STREAM,0); //creat the listing socket
bzero((char *)&servaddr ,sizeof(servaddr));
servaddr.sin_family=AF_INET; //Ipv4
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(portno);

bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));//bind the liten socket to specific port
listen(listenfd,5);
//-------------------------- server start to wait to handle requests
for(;;)
{
printf("server wait for connections\n");
bzero((char *)&cliaddr ,sizeof(cliaddr)); //clear the previus client information
clilen=sizeof(cliaddr); //client informaton container
connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
if(connfd<0)
error("error in accept this connection\n");
//--------------------------------------- //start the secret hand chacking
snprintf(buff,/*sizeof(buff)*/1,"%c",0x55);
write(connfd,buff,sizeof(buff));
//--------------------------------------- //read file name

car=0xAA;
read(connfd,buff,size(buff));
printf("client reply is %c",buff[0]);
if(car==buff[0])
{
printf("wait for file name \n");
// bzero(buff,sizeof(buff));
printf("1#%s\n",buff);
read(connfd,buff,sizeof(buff));
printf("2#%s\n",buff);
strncpy(f_name,buff,sizeof(buff)-1);
printf("file return name is %s\n",buff);
}
else
error("this is not secure client\n");

//-------------------------------------- if file found send 1 else 0
fil_fd=open(f_name,O_RDONLY); //file opent to be read only
if(fil_fd < 0)
{
snprintf(buff, sizeof(buff),"%d",0);
write(connfd,buff,1);
file("file is not found\n"); //retminate connection only
break;
}
else
{
snprintf(buff, sizeof(buff),"%d",1);
write(connfd,buff,1);
printf("file exists.Send 1 to client\nreading and send the deil\n");
//-----------------------------------reading and sending
while(read(buff,1048,fil_fd)>0)
{
printf("%s \n\n",buff);
write(connfd,buff,sizeof(buff)); //sending the file process
}//end of uploading

}//end of if_else
//-------------------------------------------- close file resources " file"
close(fil_fd);
close(connfd);
}//end of for()
//--------------------------------------------prepair the server connection
close(listenfd);
exit(0);

return 0;
}//main()

最佳答案

当您使用未定义的标识符时,您只会遇到两个简单的错误。这:

read(connfd,buff,size(buff));

应该是这样的::

read(connfd,buff,sizeof(buff));

还有这个:

file("file is not found\n");

应该是这样的:

error("file is not found\n");

当您使用这样的未定义标识符时,您的编译器无法知道它们只是错误,因为您可能已经单独编译了另一个定义它们的源文件,因此它只是假设您有名为 size( )file() 在某处定义,然后去查找它们。当它找不到它们时,它会给出“ undefined reference ”错误。

关于c - 编译时错误 "undefined reference to ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19578271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com