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!!!