2013-10-24 67 views
-3

当我编译这个文件,它不断给我错误“未定义的引用”在编译时

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() 
+8

我们可以看到导致错误的相关代码片段吗?我们是可怕的猜测者。 –

+2

没有看到任何代码,我们可以猜测。我的猜测是你错过了一个'#include'指令或需要链接一个库。 – jpw

+0

请发布有问题的源代码。 – Mauren

回答

2

你只需要两个简单的错误,你在哪里使用未定义的标识符。这:

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

应该是::

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

这:

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

应该是这样的:

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

当您使用未定义的标识符,这样,你的编译器无法知道它们是正确的错误,因为您可能单独编译了另一个定义它们的源文件,所以它只是假设您在某处定义了名为size()file()的函数,然后去查找它们。当它找不到它们时,它会给你“未定义的参考”错误。

相关问题