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:


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


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 keep getting the frustrating feeling that this should be simple...!

Many thanks!

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.

#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));

  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);


  return 0;



Recommended Answers

All 2 Replies

It's highly possible that the prototypes have changed since you last used the code or that you don't have the library required.

I don't know enough about SunOS to go any further than that ^^

Hmmmmm... If that's the case, I wonder where I need to go to figure out which libraries I need...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.