Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - undefined reference to "socket": (6 Items)
   
undefined reference to "socket"  
hi,

the project that i m working on   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


Re: undefined reference to "socket"  
-lsocket is needed for sure.
Can you post your console output after build? (command preceding the 
error message and the message).
RE: undefined reference to "socket"  
Instead of adding -lsocket to the project->options, try the following:

1) Right click on the project and bring up the Properties ...
2) On QNX C/C++ project, select the Linker Tab
3) From the drop down box, select Extra Libraries
4) Select add, and type socket. Leave the rest as default.
5) Hit apply and rebuild.

That should take care of the error. Failing that, a simple example and the
output from your project would be very helpful.

Cheers,
-Brian

> -----Original Message-----
> From: ouss zak [mailto:oussama.zakani@gmail.com] 
> Sent: Monday, April 14, 2008 14 21
> To: general-ide
> Subject: undefined reference to "socket"
> 
> hi,
> 
> the project that i m working on   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
> 
> 
> 
> 
> _______________________________________________
> General
> http://community.qnx.com/sf/go/post6750
> 
Re: RE: undefined reference to "socket"  
i got the same error , here s the console:

--------------
make -k EXTRA_SUFFIXES=cpp  all CCOPTS+=-p LDOPTS+=-p --file=C:/tmp/PCIProject2/Project_ousso/PCIProject_ouss/
QMakefile14066.tmp 
QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -oPCIProject.o PCIProject.cpp
QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -otESeries.o ./ChipObjects/tESeries.cpp
QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -otSTC.o ./ChipObjects/tSTC.cpp
QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -oPCI6025E.o PCI6025E.cpp
QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -oApplication1.o Application1.cpp
QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -oWriteNiThread.o WriteNiThread.cpp
QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -oReadNiThread.o ReadNiThread.cpp
QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -oEFamily.o EFamily.cpp
QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -oosiBus.o osiBus.cpp
QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -oosiUserCode.o osiUserCode.cpp
QCC      PCIProject.o tESeries.o tSTC.o PCI6025E.o Application1.o WriteNiThread.o ReadNiThread.o EFamily.o osiBus.o 
osiUserCode.o -oPCIProject

PCI6025E.o: In function `PCI6025E::test_Read(iBus *)':
/tmp/PCIProject2/Project_ousso/PCIProject_ouss/PCI6025E.cpp:630: undefined reference to `socket'
PCI6025E.o: In function `PCI6025E::SendTo(int, char const *, unsigned short, char const *, int)':
/tmp/PCIProject2/Project_ousso/PCIProject_ouss/PCI6025E.cpp:660: undefined reference to `sendto'
cc: C:/QNX630/host/win32/x86/usr/bin/ntox86-ld caught signal 1
make: *** [PCIProject] Error 1

QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . -olsdaq.o lsdaq.cpp
QCC      lsdaq.o -olsdaq
make: Target `all' not remade because of errors.
---------------------------------------------------

Re: undefined reference to "socket"  
I don't see any definition of libsocket on the link stage. Which type of 
project are you using? It seems to me, this is a regular make project. 
Then define -l socket  and -L ... on the link stage in your make file.


ouss zak wrote:
>
> i got the same error , here s the console:
>
> --------------
> make -k EXTRA_SUFFIXES=cpp  all CCOPTS+=-p LDOPTS+=-p 
> --file=C:/tmp/PCIProject2/Project_ousso/PCIProject_ouss/QMakefile14066.tmp

>
>
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -oPCIProject.o PCIProject.cpp
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -otESeries.o ./ChipObjects/tESeries.cpp
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -otSTC.o ./ChipObjects/tSTC.cpp
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -oPCI6025E.o PCI6025E.cpp
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -oApplication1.o Application1.cpp
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -oWriteNiThread.o WriteNiThread.cpp
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -oReadNiThread.o ReadNiThread.cpp
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -oEFamily.o EFamily.cpp
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -oosiBus.o osiBus.cpp
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -oosiUserCode.o osiUserCode.cpp
> QCC      PCIProject.o tESeries.o tSTC.o PCI6025E.o Application1.o 
> WriteNiThread.o ReadNiThread.o EFamily.o osiBus.o osiUserCode.o 
> -oPCIProject
>
> PCI6025E.o: In function `PCI6025E::test_Read(iBus *)':
> /tmp/PCIProject2/Project_ousso/PCIProject_ouss/PCI6025E.cpp:630: 
> undefined reference to `socket'
> PCI6025E.o: In function `PCI6025E::SendTo(int, char const *, unsigned 
> short, char const *, int)':
> /tmp/PCIProject2/Project_ousso/PCIProject_ouss/PCI6025E.cpp:660: 
> undefined reference to `sendto'
> cc: C:/QNX630/host/win32/x86/usr/bin/ntox86-ld caught signal 1
> make: *** [PCIProject] Error 1
>
> QCC -c -g -DkLittleEndian=1  -I ./ChipObjects -I ./Examples   -I . 
> -olsdaq.o lsdaq.cpp
> QCC      lsdaq.o -olsdaq
> make: Target `all' not remade because of errors.
> ---------------------------------------------------
>
>
>
> _______________________________________________
> General
> http://community.qnx.com/sf/go/post6753
>
Re: undefined reference to "socket"  
It looks like, for whatever reason, -lsocket is not specified.

Could you give me some insight on how did you create the project? I am 
guessing you used managed C++ project. Try adding "socket" library in 
your project properties: for managed project that should be: 
"Properties->C/C++ Build->Settings", "Tool settings" tab, "QCC 
Linker->Libraries". Under "Libraries (-l)" type "socket".

Rebuild. Post your output if it is still failing.