Thread: Unexpected Error

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    Unexpected Error

    A simple client with no errors in code gave me these errors? Any answers?

    gcc client.c
    Undefined first referenced
    symbol in file
    gethostbyname /var/tmp//ccJVrXcl.o
    socket /var/tmp//ccJVrXcl.o
    connect /var/tmp//ccJVrXcl.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
     
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
     
    int main(int argc, char *argv[])
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
     
        char buffer[256];
        if (argc < 3) {
           fprintf(stderr,"usage %s hostname port\n", argv[0]);
           exit(0);
        }
        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
     
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr,
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(portno);
     
        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr))== -1){
            error("ERROR connecting");
            exit(0);
            }
        else
        printf("Please enter the message: \n");
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        if (n < 0)
             error("ERROR writing to socket");
        bzero(buffer,256);
        n = read(sockfd,buffer,255);
        if (n < 0)
             error("ERROR reading from socket");
        printf("%s\n",buffer);
        return 0;
    }
    Last edited by Vendicate; 04-03-2006 at 08:57 PM.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    first of all fix the includes

    linker.c: In function `error':
    linker.c:10: warning: implicit declaration of function `exit'
    linker.c: In function `main':
    linker.c:24: warning: implicit declaration of function `atoi'
    linker.c:34: warning: implicit declaration of function `bzero'
    linker.c:36: warning: implicit declaration of function `bcopy'
    linker.c:41: warning: implicit declaration of function `size'
    linker.c:49: warning: implicit declaration of function `write'
    linker.c:49: warning: implicit declaration of function `strlen'
    linker.c:53: warning: implicit declaration of function `read'
    linker.c:58:2: warning: no newline at end of file
    /tmp/ccFCV3x1.o: In function `main':
    linker.c.text+0x147): undefined reference to `size'
    collect2: ld returned 1 exit status
    Size is not defined might be a typo for sizeof.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    What needs to be fixed in the includes? That is the initial reason why i posted this, i knew there was a problem with the include just by running:

    gcc client.c -E

    So any answers?
    Last edited by Vendicate; 04-03-2006 at 09:41 PM.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    Anyone?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'll assume you know what a man page is. Ok, so how about looking up each function to see what header it requires, eh?

    man 3 exit


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    I have added #include <stdlib.h> and have MANed all my functions, they all have the includes needed, but I still get the same error???

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > gcc client.c
    Code:
    gcc client.c -lsocket
    You need to specify one (or more) additional libraries to resolve the network API calls.
    I'm not at a linux machine right now to remember whether that's the right name for the library.
    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.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    Code:
    gcc client.c -lsocket
    Undefined                       first referenced
     symbol                             in file
    gethostbyname                       /var/tmp//ccQMAIhu.o  (symbol belongs to implicit dependency /usr/lib/libnsl.so.1)
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status
    This code is taken directly from http://www.cs.rpi.edu/courses/sysprog/sockets/client.c

    should work...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM