Linking library

Discussion in 'C Programming' started by Erfan, Jan 30, 2008.

  1. Erfan

    Erfan Guest

    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 返回 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
    Erfan, Jan 30, 2008
    #1
    1. Advertising

  2. Erfan

    Mark Bluemel Guest

    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?
    Mark Bluemel, Jan 30, 2008
    #2
    1. Advertising

  3. Erfan

    Erfan Guest

    Re: Linking library

    On Jan 30, 5:28 pm, Mark Bluemel <> 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
    Erfan, Jan 30, 2008
    #3
  4. 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: , ICQ: 134682867
    Wolfgang Draxinger, Jan 30, 2008
    #4
  5. Erfan

    santosh Guest

    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.
    santosh, Jan 30, 2008
    #5
  6. Erfan

    CBFalconer Guest

    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
    CBFalconer, Jan 31, 2008
    #6
  7. Re: Linking library

    Wolfgang Draxinger <> 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) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 2, 2008
    #7
  8. 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: , ICQ: 134682867
    Wolfgang Draxinger, Feb 3, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roland Raschke
    Replies:
    1
    Views:
    1,074
    Kevin Goodsell
    Sep 22, 2003
  2. Albert
    Replies:
    4
    Views:
    509
    Mike Wahler
    Sep 29, 2003
  3. Josep

    Dev-c++: Linking a library

    Josep, Mar 7, 2004, in forum: C++
    Replies:
    1
    Views:
    3,801
    John Harrison
    Mar 7, 2004
  4. Anthony Yio

    Linking to Static library of C

    Anthony Yio, May 20, 2004, in forum: C++
    Replies:
    1
    Views:
    345
    John Harrison
    May 20, 2004
  5. Thomas Ruschival

    C++/C-library linking (pslib)

    Thomas Ruschival, Nov 13, 2004, in forum: C++
    Replies:
    3
    Views:
    568
    Thomas Ruschival
    Nov 14, 2004
Loading...

Share This Page