"Symbol referencing errors" On Socket programming

Dear all,

I had the "Symbol referencing errors" while compiling a C socket code.
It said "Undefined Symbol: socketpair", but I already copy the two head files (#include "types.h", #include "socket.h") into my current directory.

Could anyone help me with it? Thanks.

By the way, I'm using Solaris, OS version is 5.10.
GCC version is 3.4.6.(is it because the GCC version is old?)
The tut2.c code I was using is from the following link:
A Socket-based IPC Tutorial

I'll copy the source code here:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h> 
#include <unistd.h>
#include <stdlib.h>

#define DATA1 "In Xanadu, did Kublai Khan..." 
#define DATA2 "A stately pleasure dome decree..."

/*
 * This program creates a pair of connected sockets, 
 * then forks and communicates over them. While this
 * is very similar to communication with pipes, socketpairs
 * are two-way communications objects. Therefore, I can
 * send messages in both directions.
 */

main()
{
        int sockets[2], child;
        char buf[1024];

        if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
                perror("opening stream socket pair");
                exit(1);
        }

        if ((child = fork()) == -1)
                perror("fork");
        else if (child) {       /* This is the parent. */
                close(sockets[0]);
                if (read(sockets[1], buf, sizeof(buf)) < 0)
                        perror("reading stream message");
                printf("-->%s\n", buf);
                if (write(sockets[1], DATA2, sizeof(DATA2)) < 0)
                        perror("writing stream message");
                close(sockets[1]);
        } else {                /* This is the child. */
                close(sockets[1]);
                if (write(sockets[0], DATA1, sizeof(DATA1)) < 0)
                        perror("writing stream message");
                if (read(sockets[0], buf, sizeof(buf)) < 0) 
                        perror("reading stream message");
                printf("-->%s\n", buf);
                close(sockets[0]);
        }
}

Remember that #include files only declare that the symbols exist -- they don't actually import them. "undeclared", a compilation error, is a totally different error than "undefined", a linking error.

Solaris apparently needs -lsocket and -lnsl. Slightly unusual.

1 Like

Thank you Corona and nice to see you again!

Seems that I need do some homework, since I know nothing about -lsocket and -lnsl.

Solaris man pages tend to be a lot more complete than Linux man pages. Note the required libraries are actually listed on the man page for library calls:

Sockets Library Functions                     socketpair(3SOCKET)

NAME
     socketpair - create a pair of connected sockets

SYNOPSIS
     cc [ flag ... ] file ... -lsocket  -lnsl  [ library ... ]
     #include <sys/types.h>
     #include <sys/socket.h>

     int socketpair(int domain, int type, int protocol, int sv[2]);

DESCRIPTION
     The socketpair() library call creates  an  unnamed  pair  of
     connected sockets in the specified address family domain, of
     the specified type, that uses the optionally specified  pro-
     tocol.  The descriptors that are used in referencing the new
     sockets are returned in sv[0] and sv[1]. The two sockets are
     indistinguishable.

RETURN VALUES
     socketpair() returns -1 on failure and 0 on success.

ERRORS
     The call succeeds unless:

     EAFNOSUPPORT       The specified address family is not  sup-
                        ported on this machine.

     EMFILE             Too many descriptors are in use  by  this
                        process.

     ENOMEM             There was insufficient  user  memory  for
                        the operation to complete.

     ENOSR              There were insufficient STREAMS resources
                        for the operation to complete.

     EOPNOTSUPP         The specified protocol does  not  support
                        creation of socket pairs.

     EPROTONOSUPPORT    The specified protocol is  not  supported
                        on this machine.

     EACCES             The process  does  not  have  appropriate
                        privileges.

SunOS 5.11          Last change: 10 Jan 2001                    1

Sockets Library Functions                     socketpair(3SOCKET)

ATTRIBUTES
     See attributes(5) for descriptions of the  following  attri-
     butes:

     ____________________________________________________________
    |       ATTRIBUTE TYPE        |       ATTRIBUTE VALUE       |
    |_____________________________|_____________________________|
    | MT-Level                    | Safe                        |
    |_____________________________|_____________________________|

SEE ALSO
     pipe(2), read(2), write(2), socket.h(3HEAD), attributes(5)

NOTES
     This call is currently  implemented  only  for  the  AF_UNIX
     address family.

SunOS 5.11          Last change: 10 Jan 2001                    2
1 Like

I've frequently seen libraries listed in linux man pages as well. Admittedly not always.