Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C network programming compile questions

Status
Not open for further replies.

chz013

Programmer
Jan 5, 2003
73
0
0
US
Following code is shown:

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

int
main(int argc,char **argv) {
int z; /* Status return code */
int s[2]; /* Pair of sockets */

/*
* Create a pair of local sockets :
*/
z = socketpair(AF_UNIX,SOCK_STREAM,0,s);

if ( z == -1 ) {
fprintf(stderr,&quot;%s: socketpair(AF_UNIX,SOCK_STREAM,0)\n&quot;, strerror(errno));


return 1; /* Failed */
}

/*
* Report the socket file descriptors returned:
*/
printf(&quot;s[0] = %d;\n&quot;,s[0]);
printf(&quot;s[1] = %d;\n&quot;,s[1]);

system(&quot;netstat | grep tcp&quot;);

return 0;
}
-----------------------------------------------
I then tried compiling it,
tex15 101 % make 01lst01
gcc -c -D_GNU_SOURCE -Wall 01LST01.c
01LST01.c:2: warning: #warning SOL forced
gcc 01LST01.o -o 01lst01
01LST01.o: In function `main':
01LST01.o(.text+0x1c): undefined reference to `socketpair'
collect2: ld returned 1 exit status
make: *** [01lst01] Error 1


why is that ?
I checked /usr/include dir and the header files are there.

Any help is FULLY Appreciated
 
i guess you need to link with the socket library, add -lsocket to the link line:
gcc 01LST01.o -o 01lst01 -lsocket
 
Ranganath
thanks for your response/help.
you're right about the options.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top