Symbol referencing errors

This is a discussion on Symbol referencing errors within the C++ Programming forums, part of the General Programming Boards category; hello everyone, I need help. i am trying out beejs guide to socket programming. I get the following error while ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    4

    Symbol referencing errors

    hello everyone,

    I need help. i am trying out beejs guide to socket programming.
    I get the following error while trying to compile my code.


    Undefined first referenced
    symbol in file
    __xnet_socket /var/tmp//ccdwR3mW.o
    listen /var/tmp//ccdwR3mW.o
    inet_aton /var/tmp//ccdwR3mW.o
    __xnet_bind /var/tmp//ccdwR3mW.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status


    The code is given below


    Code:
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <iostream>
    using namespace std;
    
    #define ServerIP "206.236.134.28"
    #define MYPORT 3490    // the port users will be connecting to
    #define BACKLOG 10     // how many pending connections queue will hold
    
    int main()
    {
        int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
        struct sockaddr_in my_addr;    // my address information
        struct sockaddr_in their_addr; // connector's address information
        int sin_size;
    
        sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
    
        my_addr.sin_family = AF_INET;         // host byte order
        my_addr.sin_port = htons(MYPORT);     // short, network byte order
        if(!inet_aton(ServerIP,&(my_addr.sin_addr)))
          cout<<"\nError: Unable to assigin address"<<endl;
        memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
    
        // don't forget your error checking for these calls:
        bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
    
        listen(sockfd, BACKLOG);
       // sin_size = sizeof(struct sockaddr_in);
        //new_fd = accept(sockfd, (struct sockaddr *)&their_addr, (socklen_t*)&sin_size);
        //int recv(int new_fd, void *buf, int len, unsigned int flags);
        //cout<<"\nThe buffer = "<<buf<<" *buf "<<*buf;
        return 0;
    }
    Code:
     
    i am comiling it as
    #g++ <file name>

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    Quote Originally Posted by n00pster View Post
    hello everyone,

    I need help. i am trying out beejs guide to socket programming.
    I get the following error while trying to compile my code.

    ...

    i am comiling it as
    #g++ <file name>
    I used gcc version 3.4.4 (g++ -Wall name.c) and it compiled fine.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    But which OS (and version) are you compiling for?

    I hope that '#' doesn't mean you're compiling as 'root'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    What platform are you on? On some plats you need to link with -lsocket

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    Quote Originally Posted by Salem View Post
    But which OS (and version) are you compiling for?

    I hope that '#' doesn't mean you're compiling as 'root'
    good point...freeBSD 6

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    Quote Originally Posted by brewbuck View Post
    What platform are you on? On some plats you need to link with -lsocket
    Thanks a lot for all the replies.

    I am compliling on solaris
    #uname -a
    SunOS ipksun019 5.8 Generic_117350-22 sun4u sparc SUNW,Sun-Fire-480R Solaris

    I am not logged in as root. My PS1 is different and also its little bit long. So i replaced it with '#' after pasting it. :-)

    I tried to compile with -lsocket option and the number of Symbol referencing errors reduced to 1.

    Please see below:
    #g++ -lsocket dummyServer.cpp
    Undefined first referenced
    symbol in file
    inet_aton /var/tmp//ccbQ14qj.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    > g++ -lsocket dummyServer.cpp
    Order matters
    g++ dummyServer.cpp -lsocket
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    Quote Originally Posted by Salem View Post
    > g++ -lsocket dummyServer.cpp
    Order matters
    g++ dummyServer.cpp -lsocket
    I got the same old error.

    #g++ dummyServer.cpp -lsocket
    Undefined first referenced
    symbol in file
    inet_aton /var/tmp//ccwzWCDb.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

    My guess is that i have to link some other lib too. :-(

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    thanks a lot, its working.
    -lnsl -lsocket -lresolv is needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 03:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21