Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Linking library

Reply
Thread Tools

Linking library

 
 
Erfan
Guest
Posts: n/a
 
      01-30-2008
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+0x7: undefined reference to `frintf'
getdate_udp.c.text+0x160): undefined reference to `sento'
collect2: ld 返回 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
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      01-30-2008
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+0x7: 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?
 
Reply With Quote
 
 
 
 
Erfan
Guest
Posts: n/a
 
      01-30-2008
On Jan 30, 5:28 pm, Mark Bluemel <(E-Mail Removed)> 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+0x7: 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
 
Reply With Quote
 
Wolfgang Draxinger
Guest
Posts: n/a
 
      01-30-2008
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: http://www.velocityreviews.com/forums/(E-Mail Removed), ICQ: 134682867

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      01-30-2008
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+0x7: 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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-31-2008
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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-02-2008
Wolfgang Draxinger <(E-Mail Removed)> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Wolfgang Draxinger
Guest
Posts: n/a
 
      02-03-2008
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: (E-Mail Removed), ICQ: 134682867

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl MakeMaker - how to force Perl linking with the static C library (libcrt.lib) instead of dynamic C library (msvcrt.lib) Avi Perl Misc 0 04-17-2007 09:20 PM
Linking to Static library of C Anthony Yio C++ 1 05-20-2004 07:36 AM
Dev-c++: Linking a library Josep C++ 1 03-07-2004 08:48 AM
Q: can't find C++ symbols if gcc linking with Sun C++ library Albert C++ 4 09-29-2003 04:03 PM
Linking static library including template Roland Raschke C++ 1 09-22-2003 07:02 PM



Advertisments