E
Erfan
Hi,come.lang.c:
I am learning a Socket programe ,here is the code:
//learn Socket ,this belongs UDP
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
char *host;
int sockfd;
int len,result;
struct sockaddr_in address;
struct hostent *hostinfo;
struct servent *servinfo;
char buffer[128];
if(argc==1) //test parameters
host="127.0.0.1";
else
host=argv[1];
hostinfo=gethostbyname(host); //find Host address
if(!hostinfo)
{ frintf(stderr,"no host :%s\n,host");
exit(1);
}
servinfo=getservbyname("daytiem","udp"); //check service
exist
if(!servinfo)
{
fprintf(stderr,"no daying time service");
exit(1);
}
sockfd=socket(AF_INET,SOCK_DGRAM,0); //creat UDP socket
address.sin_family=AF_INET; //consruct the address for
use sento and recvfrom
address.sin_port=servinfo->s_port;
address.sin_addr=*(struct in_addr *)*hostinfo->h_addr_list;
len=sizeof(address);
result=sento(sockfd,buffer,1,0,(struct
sockaddr*)&address,len);
result=recvfrom(sockfd,buffer,sizeof(buffer),0,(struct
sockaddr *)&address,&len);
buffer[result]='\0';
printf("read %d bytes: %s",result,buffer);
close(sockfd);
exit(0);
}
----------------------------
my question is not about UDP,but the action of linking. i use GCC
to compile,it puts like this:
/tmp/ccyrQ2Pf.o: In function `main':
getdate_udp.c.text+0x78): undefined reference to `frintf'
getdate_udp.c.text+0x160): undefined reference to `sento'
collect2: ld $BJV2s(B 1
---
yeah, i know i miss the right library to link. So i check it in the
comp.c.faq,13.23,13.24,13.25.
however, i become mess about when to link,and what library to link.
For instance,i use <math.h>,
so we have to use -lm to link the library . what about others? in
this code, i use<sys/socket.h>,
which order should i add? Is there any different between "share
library"and "alone library"?
why "printf" does not need to link his library,and only his
<stdio.h>is enough?
Best wishes
I am learning a Socket programe ,here is the code:
//learn Socket ,this belongs UDP
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
char *host;
int sockfd;
int len,result;
struct sockaddr_in address;
struct hostent *hostinfo;
struct servent *servinfo;
char buffer[128];
if(argc==1) //test parameters
host="127.0.0.1";
else
host=argv[1];
hostinfo=gethostbyname(host); //find Host address
if(!hostinfo)
{ frintf(stderr,"no host :%s\n,host");
exit(1);
}
servinfo=getservbyname("daytiem","udp"); //check service
exist
if(!servinfo)
{
fprintf(stderr,"no daying time service");
exit(1);
}
sockfd=socket(AF_INET,SOCK_DGRAM,0); //creat UDP socket
address.sin_family=AF_INET; //consruct the address for
use sento and recvfrom
address.sin_port=servinfo->s_port;
address.sin_addr=*(struct in_addr *)*hostinfo->h_addr_list;
len=sizeof(address);
result=sento(sockfd,buffer,1,0,(struct
sockaddr*)&address,len);
result=recvfrom(sockfd,buffer,sizeof(buffer),0,(struct
sockaddr *)&address,&len);
buffer[result]='\0';
printf("read %d bytes: %s",result,buffer);
close(sockfd);
exit(0);
}
----------------------------
my question is not about UDP,but the action of linking. i use GCC
to compile,it puts like this:
/tmp/ccyrQ2Pf.o: In function `main':
getdate_udp.c.text+0x78): undefined reference to `frintf'
getdate_udp.c.text+0x160): undefined reference to `sento'
collect2: ld $BJV2s(B 1
---
yeah, i know i miss the right library to link. So i check it in the
comp.c.faq,13.23,13.24,13.25.
however, i become mess about when to link,and what library to link.
For instance,i use <math.h>,
so we have to use -lm to link the library . what about others? in
this code, i use<sys/socket.h>,
which order should i add? Is there any different between "share
library"and "alone library"?
why "printf" does not need to link his library,and only his
<stdio.h>is enough?
Best wishes