|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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,(st ruct 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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Erfan wrote:
[snip] > { frintf(stderr,"no host :%s\n,host"); [snip] > result=sento(sockfd,buffer,1,0,(struct > 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'Why don't you read the error messages a few times before posting? Where do you expect a "frintf" function (as opposed to "fprintf") and a "sento" function (as opposed to "sendto") to be defined? |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Jan 30, 5:28 pm, Mark Bluemel <mark_blue...@pobox.com> wrote:
> Erfan wrote: > > [snip] > > > > > { frintf(stderr,"no host :%s\n,host"); > [snip] > > result=sento(sockfd,buffer,1,0,(struct > > 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'> > Why don't you read the error messages a few times before posting? > > Where do you expect a "frintf" function (as opposed to "fprintf") > and a "sento" function (as opposed to "sendto") to be defined? :{ It`s a shame for my Carelessness,thank you Mark |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Erfan wrote:
> :{ It`s a shame for my Carelessness,thank you Mark Because of this you should IMHO always have -Werror-implicit-function-declaration in your CFLAGS. Wolfgang Draxinger -- E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Erfan wrote:
> Hi,come.lang.c: > I am learning a Socket programe ,here is the code: <snip> > 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 ?? 1 These are caused by typographical errors in your code. "frintf" should probably be fprintf(), which is declared in stdio.h and "sento" should be probably be sendto() which is declared in sys/socket.h for POSIX compliant systems. > 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? Usually under POSIX capable systems the "system C library" provides most of these functions. This is automatically linked in by default for most compilers under most configurations. Only the mathematical functions of the C library need a separate '-lm' switch, mainly for historical reasons. The C library file itself is likely to be named something like 'libc.XXX', where the XXX portion will definitely vary according to the library's version and whether it's shared or static and on other details. > Is there any different between "share > library"and "alone library"? Yes. But please ask such systems specific details in a more suitable group like <news:comp.unix.programmer> for UNIX and POSIX programming, <news:comp.os.linux.development.apps> for application programming for Linux systems and <news:comp.os.ms-windows.programmer.win32> for Windows programming. There are also many more groups, please search your news server's group list file. All the questions you have asked thus far are strictly speaking not topical here since Standard C says nothing about specific compilers, sockets, linking and libraries. > why "printf" does not need to link his library,and only his > <stdio.h>is enough? > Best wishes The core C library is usually linked in by default, unless you specify otherwise for most compilers. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Erfan wrote:
> > 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> The only .h file of the above present in standard C is stdio.h. That makes this off-topic for comp.lang.c. Try comp.unix.programmer. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. -- Posted via a free Usenet account from http://www.teranews.com |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Wolfgang Draxinger <wdraxinger@darkstargames.de> writes:
> Erfan wrote: > >> :{ It`s a shame for my Carelessness,thank you Mark > > Because of this you should IMHO always have > > -Werror-implicit-function-declaration > > in your CFLAGS. Perhaps. But as long as *you* treat warnings as serious errors, it doesn't matter so much how the compiler treats them. -- Keith Thompson (The_Other_Keith) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Keith Thompson wrote:
> Perhaps. But as long as *you* treat warnings as serious > errors, it doesn't matter so much how the compiler treats them. Unfortunately warnings are not errors, which will fool build systems. With -Werror-implicit-function-declaration the build process fails already at the compilation stage of the affected source file and stop there. If it's just treated as a warning the error happens at the linking stage, with the warning message eventually having scrolled off the buffer, it it's a full build. The good thing is, GCC (and some other compilers/linkers) can report, in which source file and line the unresolved symbol was used, but that's only if such information has been retained in the object files. If those got striped before linking you're out of luck. Wolfgang Draxinger -- E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867 |
|
![]() |
| Outils de la discussion | |
|
|