PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Linking library
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Linking library

Réponse
 
LinkBack Outils de la discussion
Vieux 30/01/2008, 09h21   #1
Erfan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Linking library

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
  Réponse avec citation
Vieux 30/01/2008, 09h28   #2
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Linking library

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?
  Réponse avec citation
Vieux 30/01/2008, 09h54   #3
Erfan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Linking library

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
  Réponse avec citation
Vieux 30/01/2008, 10h40   #4
Wolfgang Draxinger
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Linking library

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

  Réponse avec citation
Vieux 30/01/2008, 17h04   #5
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Linking library

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.

  Réponse avec citation
Vieux 31/01/2008, 02h05   #6
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Linking library

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

  Réponse avec citation
Vieux 02/02/2008, 21h43   #7
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Linking library

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"
  Réponse avec citation
Vieux 03/02/2008, 16h01   #8
Wolfgang Draxinger
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Linking library

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18108 seconds with 16 queries