View topic - Can't compile socket() function successfully!!

Can't compile socket() function successfully!!

anything that doesn't fit to other groups.

Can't compile socket() function successfully!!

Postby ganson » Tue Jan 13, 2004 3:26 am

Hi~~all.
I am so sorry for asking questions so frequently! :oops:
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. :shock:

Do I miss something ? :?:

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;
}
ganson
Active Member
 
Posts: 10
Joined: Sun Dec 14, 2003 10:52 am

Postby rick » Tue Jan 13, 2004 3:55 am

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.
rick
QNX Master
 
Posts: 500
Joined: Wed Nov 13, 2002 3:59 am

Postby ganson » Tue Jan 13, 2004 4:59 am

Great!!It works.
Thanks a lot!!!!
ganson
Active Member
 
Posts: 10
Joined: Sun Dec 14, 2003 10:52 am


Return to General Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron