Thread: "Undefined Symbol" when compiling a socket program?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14

    Question "Undefined Symbol" when compiling a socket program?

    Hi everyone,

    I have a "This should be simple!" kind of problem. I need to do a little socket programming on a SunOS machine. I went back to an old school assignment I did years ago, cut-n-pasted that code, intending to basically cannibalize it for the program I need to write now. Trouble is, my compiler chokes on the socket-specific terms like getaddrinfo() or inet_ntop.

    Here is the output when I try to compile:

    ----------------------------------------------------------------------------
    Code:
    bash-3.00$ g++ Main.cpp
    Undefined                       first referenced
     symbol                             in file
    getaddrinfo                         /var/tmp//ccCpuGRv.o
    freeaddrinfo                        /var/tmp//ccCpuGRv.o
    inet_ntop                           /var/tmp//ccCpuGRv.o
    gai_strerror                        /var/tmp//ccCpuGRv.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status
    bash-3.00$
    ----------------------------------------------------------------------------


    I'll post the latest code I'm using, but I don't think my problem lies in the code. I've tried compiling my own code plus about a dozen examples I've found online. I *ALWAYS* get the same problem, this "undefined symbol" problem with some socket-specific terms. I've also tried alternating between g++ and gcc; both compilers essentially complain about the same "undefined symbol" problem.

    (As a side note, I can copy the EXACT SAME CODE over to my school's Linux machine, and it compiles just fine there!)

    So I'm thinking this is (A) a problem with the SunOS machine I'm working on, or (B) I am forgetting a library or something. I'm hoping it is not (A), as I'm not the system admin and don't have the power to install anything on this platform.

    Has anyone seen something like this? I need to know what the problem is... and how do I get around it???

    Many thanks!
    -Pete


    Below is the code I am compiling in the above example. Full disclosure: This is an example I found online which I have been fiddling with. It compiles on the Linux machine.
    ----------------------------------------------------------------------------
    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <cstdlib>
    #include <arpa/inet.h>
    #include <map>
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <time.h>
    #include <vector>
    #include <algorithm>
    #include <queue>
    #include <cstring>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <cstdio>
    #include <time.h>
    #include <map>
    using namespace std;
    
    int main(int argc, char **argv) {
    
      //Usage of getaddrinfo()
    
      int status;
      struct addrinfo hints, *p;
      struct addrinfo *servinfo;  //will point to the result
      char ipstr[INET6_ADDRSTRLEN];
    
      memset(&hints, 0, sizeof hints); // make sure the struct is empty
    
      hints.ai_family   = AF_UNSPEC;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_flags    = AI_PASSIVE;
    
      if ((status = getaddrinfo(NULL, "4321", &hints, &servinfo)) == -1) {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
      }
    
      for (p=servinfo; p!=NULL; p=p->ai_next) {
        struct in_addr  *addr;
        if (p->ai_family == AF_INET) {
          struct sockaddr_in *ipv = (struct sockaddr_in *)p->ai_addr;
          addr = &(ipv->sin_addr);
        }
        else {
          struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
          addr = (struct in_addr *) &(ipv6->sin6_addr);
        }
        inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
    
      }
      cout<<"Address:"<<ipstr<<endl;
      freeaddrinfo(servinfo);
    
      return 0;
    
    }
    ----------------------------------------------------------------------------

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14
    Ah HA! I'd just reread Beej's Guide but skipped that part. Serves me right.

    FYI for anyone following this thread: Beej's advises adding: "-lnsl -lsocket -lresolv" to your compiler command:

    $ cc Main.c -lnsl -lsocket -lresolv

    So the note in Beej's is for compiling with the cc compiler. (Using C compiler) Anyone know if there's a similar fix for one of the C++ compilers? (preferably g++ or gcc?)

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by phummon View Post
    So the note in Beej's is for compiling with the cc compiler. (Using C compiler) Anyone know if there's a similar fix for one of the C++ compilers? (preferably g++ or gcc?)
    It should be no different.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14
    Figured it out - a colleague explained to me how to properly read the man pages. What I needed was this:

    For each socket term the compiler was complaining about, I had to add in the libraries required and include the flags necessary in my compile statement.

    For example, for "getaddrinfo":

    From the man page:
    SYNOPSIS
    cc [ flag... ] file ... -lsocket -lnsl [ library ... ]
    #include <sys/socket.h>
    #include <netdb.h>


    So I added <sys/socket.h> and <netdb.h> as libraries and included "-lsocket -lnsl" with my compile statement:
    g++ -g -Wall -ansi -pg -D_DEBUG_ -lsocket -lnsl Main.cpp -o exe

    Solved!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C sharp program
    By pokhara in forum C# Programming
    Replies: 4
    Last Post: 07-29-2009, 01:44 PM
  2. C compiling / error: stray ‘\’ in program
    By Elya in forum C Programming
    Replies: 5
    Last Post: 07-02-2009, 08:20 AM
  3. multi-thread socket program with packet fragmentation
    By thinkerchjf in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-25-2009, 10:46 AM
  4. Replies: 4
    Last Post: 03-10-2008, 07:03 AM
  5. Replies: 7
    Last Post: 12-14-2003, 01:00 PM

Tags for this Thread