undefined reference to "socket"


i am not sure if i am in the right section.

the project that i m working on qnx reads analog inputs and then it should send the data by UDP.

when i m compiling the project, i get this error :
"undefined reference to “socket” "

but i have already included socket.h. I thought at first it could be a problem in the order of includes.

#include <sys/socket.h>
#include <sys/select.h>

#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>

i also tried to add -lsocket in the linker (project->options).

here’s a part of my code :

    sockd = socket(AF_INET, SOCK_DGRAM, 0); 

unsigned short int port = 20000; 
SendTo(sockd, (const char*)TargetHost, port,codec.msgBuffer, sizeof(codec.msgBuffer)+1); 

int PCI6025E::SendTo(int sock, const char *host, unsigned short int port, const char *buffer, int size)
struct sockaddr_in addr;
struct hostent *hp ;
int v;

	addr.sin_addr.s_addr = inet_addr((const char*)host);
   memset(&addr, 0, sizeof(addr)); 
   addr.sin_family = AF_INET; 
   addr.sin_port = port; 

   return sendto(sock, buffer, size, 0, (struct sockaddr *)&addr, sizeof(addr)); 


If someone could help resolve this problem it will be very appreciated.
Thank you

Hello Ouss,
Please see below successfully compiled code fragment with socket.
Take care,

#include <sys/socket.h>
#include <sys/select.h>

#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>

int main ()
int sockd;
sockd = socket(AF_INET, SOCK_DGRAM, 0);
return (1);

make -k all --file=C:/Yuriy_on_C/workspace_HELLO/hello/QMakefile62219.tmp
make -j 1 -Cx86 -fMakefile all
make[1]: Entering directory C:/Yuriy_on_C/workspace_HELLO/hello/x86' make -j 1 -Co -fMakefile all make[2]: Entering directory C:/Yuriy_on_C/workspace_HELLO/hello/x86/o’
C:/QNX630/host/win32/x86/usr/bin/qcc -Vgcc_ntox86 -c -Wc,-Wall -Wc,-Wno-parentheses -O -lsocket -DNDEBUG -I. -IC:/Yuriy_on_C/workspace_HELLO/hello/x86/o -IC:/Yuriy_on_C/workspace_HELLO/hello/x86 -IC:/Yuriy_on_C/workspace_HELLO/hello -IC:/QNX630/target/qnx6/usr/include C:/Yuriy_on_C/workspace_HELLO/hello/hello.c
C:/QNX630/host/win32/x86/usr/bin/rm -f C:/Yuriy_on_C/workspace_HELLO/hello/x86/o/hello
C:/QNX630/host/win32/x86/usr/bin/qcc -Vgcc_ntox86 -lsocket -oC:/Yuriy_on_C/workspace_HELLO/hello/x86/o/hello hello.o -L. -LC:/QNX630/target/qnx6/x86/lib -LC:/QNX630/target/qnx6/x86/usr/lib
make[2]: Leaving directory C:/Yuriy_on_C/workspace_HELLO/hello/x86/o' make[1]: Leaving directory C:/Yuriy_on_C/workspace_HELLO/hello/x86’

Do you have bound to socketlibrary? not only included.


Hello ouss,

I have the same problem… I tried the simple code of ysinitsky but it tells me undefined reference to ‘socket’…any ideas?

I even get the help for this socket function when I do a ‘ctrl+space bar’

You need to add the socket library -lsocket