Can't compile socket() function successfully!!

Hi~~all.
I am so sorry for asking questions so frequently! :blush:
While I compile my program,It shows the error as below.

gcc serv.c -o test

/tmp/ccaWzxh3.o: In function main': /tmp/ccaWzxh3.o(.text+0x1f): undefined reference to socketā€™
collect2: ld returned 1 exit status

I have included the ā€œsocket.hā€ file,But I donā€™t know what is the matter.

I also use another socket function like ā€œlisten()ā€ to tset for compiling and i

it shows the same result. :open_mouth:

Do I miss something ? :question:

Here is my program.

#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>

int main(int argc , char *argv[]){
int errvalue;
int hSocket;

errno = EOK;

hSocket=socket( AF_INET,SOCK_STREAM,0 );

if(hSocket<0){
errvalue = errno;
printf( ā€œThe error generated was %d\nā€, errvalue );
printf( ā€œThat means: %s\nā€, strerror( errvalue ) );
return 1;
}
printf(ā€œOpen socket successfully\nā€);
close(hSocket);
return 1;
}

try adding -lsocket to the end of your compile line:

$ qcc serv.c -o servtest -l socket

A couple other points.

  1. Use qcc rather than gcc - at some point it will save you a lot of problems.
  2. Donā€™t name your test program ā€˜testā€™ . That is also the name of a builtin shell function and it can be real painful to track down when you try to test something and it doesnā€™t work.
  3. Donā€™t work as root - mistakes are way to costly. If you need to be root to run something, use the ā€˜opā€™ command or leave a window open with root privs.

Great!!It works.
Thanks a lot!!!