Linking library

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
 
M

Mark Bluemel

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?
 
E

Erfan

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
 
W

Wolfgang Draxinger

Erfan said:
:{ 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
 
S

santosh

Erfan said:
Hi,come.lang.c:
I am learning a Socket programe ,here is the code:

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 <for UNIX and POSIX programming,
<for application programming for
Linux systems and <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.
 
C

CBFalconer

Erfan said:
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.
 
K

Keith Thompson

Wolfgang Draxinger said:
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.
 
W

Wolfgang Draxinger

Keith said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top