I am getting the following errors when i gcc my code:

44) unixs1 $ gcc whoisserver.c
whoisserver.c: In function `main':
whoisserver.c:59: warning: passing arg 2 of `bind' from incompatible pointer type
whoisserver.c:71: warning: passing arg 2 of `accept' from incompatible pointer type
Undefined                       first referenced
 symbol                             in file
bind                                /var/tmp//ccSx8OhK.o
getservbyname                       /var/tmp//ccSx8OhK.o
accept                              /var/tmp//ccSx8OhK.o
listen                              /var/tmp//ccSx8OhK.o
gethostbyname                       /var/tmp//ccSx8OhK.o
socket                              /var/tmp//ccSx8OhK.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

I know my libraries are correct and was looking for help resolving the undefined symbols.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pwd.h>
#include <strings.h>
#include <unistd.h>


#define BACKLOG          5          /* # of requests to queue              */
#define MAXHOSTNAME      32         /* maximum host name length            */

int main(argc,argv)
int argc;
char *argv[];
{
   int s, t;                        /* socket descriptor                   */
   int i;                           /* general purpose integer             */
   u_short portbase =0;
   struct sockaddr_in sa, isa;      /* internet socket structure           */
   struct hostent *hp;              /* host name structure                 */
   char *myname;                    /* pointer to the name of this program */
   struct servent *sp;              /* pointer to srevice entry            */
   char localhost[MAXHOSTNAME+1];   /* local host name as character string */

   myname = argv[0];
   portbase = 8000;
   /*
    * Look up the WHOIS service entry
    */
   if ((sp = getservbyname("whois","tcp")) == NULL)
   {
      fprintf(stderr,"%s: No whois service on this host.\n",myname);
      exit(1);
   }   

   gethostname(localhost,MAXHOSTNAME);
   if ((hp = gethostbyname(localhost)) == NULL)
   {
      fprintf(stderr,"%s: Cannnot get local host info?\n",myname);
      exit(1);
   }


   sa.sin_port = sp->s_port;
   sa.sin_port = htons(ntohs((u_short)sp->s_port)+portbase);
   bcopy((char *)hp->h_addr,(char *)&sa.sin_addr,hp->h_length);
   sa.sin_family = hp->h_addrtype;


   if ((s = socket(hp->h_addrtype,SOCK_STREAM,0)) < 0)
   {
      perror("socket");
      exit(1);
   }

    if (bind(s, &sa, sizeof sa) < 0)
    {
       perror("bind");
       exit(1);
    }

    listen(s, BACKLOG);

   while (1)
   {
      i = sizeof sa;

      if ((t = accept(s,&isa,&i)) < 0)
      {
         perror("accept");
         exit(1);
      }
      whois(t);      /* perform the actual WHOIS service */
      close(t);
   }
}


int whois(int sock);
{
   struct passwd *p;
   char buf[BUFSIZ+1];
   int i;


   if ((i = read(sock,buf,BUFSIZ)) <= 0)
      return;
   buf[i] = '\0';

   if ((p = getpwnam(buf)) == NULL)
       strcpy(buf,"User not found\n");
   else

       sprintf(buf,"%s: %s\n",p->pw_name,p->pw_gecos);

   write(sock,buf,strlen(buf));
   return;
}

Thanks for the help.

Dani AI

Generated

Quick diagnosis and next steps (Solaris vs Linux). The linker errors listing socket, bind, accept, listen, gethostbyname etc. usually mean either (a) the platform requires extra network libraries at link time (Solaris historically requires the socket/nsl libraries), or (b) on Linux the problem is more likely a code-level mismatch that leaves symbols unresolved at link time. As noted, Solaris man pages show the networking man-pages list -lsocket -lnsl in the SYNOPSIS; on Linux these calls are provided by the standard C library (libc) so extra link flags are normally unnecessary. (docs.oracle.com)

Fix the immediate compiler warnings (bind / accept). bind() and accept() expect a struct sockaddr * parameter; passing a struct sockaddr_in * without a cast causes the “incompatible pointer type” warnings. Use an explicit cast so the calls match the signature, for example:

if (bind(s, (struct sockaddr *)&sa, sizeof sa) < 0) { ... }

if ((t = accept(s, (struct sockaddr *)&isa, &i)) < 0) { ... }

The man pages show those prototypes and explain the addr argument purpose. (man7.org)

Other likely causes seen in the posted code: the whois definition appears malformed (a stray prototype plus a block like int whois(int sock); { ... }) — that should be a proper definition int whois(int sock) { ... } so the symbol is actually defined. Compile with warnings enabled (-Wall -Wextra -std=c99) to catch problems early. Replace legacy bcopy() with memcpy()/memmove() (portable and not deprecated), and prefer getaddrinfo()/getnameinfo() over the older gethostbyname/getservbyname APIs for IPv6-safety and reentrancy. (gnu.org)

Practical checklist (summary):

  • Confirm OS: Solaris → add platform link libs (place after objects); Linux/BackTrack → no special link flags normally. (docs.oracle.com)
  • Fix casts on bind/accept. (man7.org)
  • Fix the whois function definition (remove stray prototype before the body). (gnu.org)
  • Replace bcopy and migrate name/address calls to getaddrinfo where possible. (man7.org)

References above point to the relevant man pages and language notes for validation.

Recommended Answers

All 2 Replies

You are on Solaris, right? Add -lsocket -lnsl to the command line. That will pick up the necessary libraries.

I am using Backtrack and compiling through nano

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.