Saturday, June 2, 2012

Compiling Happycoders libsocket in Ubuntu


 Happycoders libsocket is an object oriented and platform independent socket library for C++. It has a nice structure for both tcp and udp sockets. It runs like a charm if you want to connect a server and get the content as well as listen from a port and accept the connections.

Since package maintainers published a debian package mainly for Debian and Ubuntu distros, it can easly be installed on those systems. If you use another os the solutions is to compile it yourself. However, GCC throws an error message which can be easily solved. In order to compile the package and use it with your C++ applications, first open an console and type


wget http://www.speedblue.org/conf/libsocket-1.8.tar.gz


so we have the source package downloaded. Extract the tar archive

gunzip libsocket-1.8.tar.gz
tar -xvf libsocket-1.8.tar 


Now we have a folder with a name of libsocket-1.8. Get in this folder and start the compilation process:

./configure


and then type

make


After getting some output, GCC throws an error:

make  all-recursive
make[1]: Entering directory `/tmp/socket/libsocket-1.8'
Making all in src
make[2]: Entering directory `/tmp/socket/libsocket-1.8/src'
Making all in tests
make[3]: Entering directory `/tmp/socket/libsocket-1.8/src/tests'
make[3]: Nothing to be done for `all'.
make[3]: Leaving directory `/tmp/socket/libsocket-1.8/src/tests'
make[3]: Entering directory `/tmp/socket/libsocket-1.8/src'
if /bin/bash ../libtool --mode=compile g++ -DHAVE_CONFIG_H -I. -I. -I.. -I../src    -g -O2 -Wall -ansi -pedantic -MT socket.lo -MD -MP -MF ".deps/socket.Tpo" \
   -c -o socket.lo `test -f 'socket.cc' || echo './'`socket.cc; \
 then mv -f ".deps/socket.Tpo" ".deps/socket.Plo"; \
 else rm -f ".deps/socket.Tpo"; exit 1; \
 fi
 g++ -DHAVE_CONFIG_H -I. -I. -I.. -I../src -g -O2 -Wall -ansi -pedantic -MT socket.lo -MD -MP -MF .deps/socket.Tpo -c socket.cc  -fPIC -DPIC -o .libs/socket.o
socket.cc: In member function 'void Network::Socket::_write_str_bin(int, const string&) const':
socket.cc:238:44: error: 'memcpy' was not declared in this scope
make[3]: *** [socket.lo] Error 1
make[3]: Leaving directory `/tmp/socket/libsocket-1.8/src'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/tmp/socket/libsocket-1.8/src'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/tmp/socket/libsocket-1.8'
make: *** [all] Error 2


When you look into to error message we can see the key point:

socket.cc: In member function 'void Network::Socket::_write_str_bin(int, const string&) const':
socket.cc:238:44: error: 'memcpy' was not declared in this scope


It is clear that, GCC needs memcpy to be declared before it is used. It is a function defined in the standar C package string.h and it cstring in C++. Simply open the socket.cc and add the line

#include <string.h>


and compile again. After typing the make command you will get a new error message. A part of this error message is given below:

udpsocket.cc: In member function 'virtual std::string Network::UdpSocket::_read_line_bin(int, unsigned int)':
udpsocket.cc:68:29: error: 'memset' was not declared in this scope


and this is a similar error. memset is an other standard function defined in string.h in C++ and C. Simple edit the file udpsocket and add the same header

#include <string.h>


After compiling many times, GCC will throw similar errors. To cope with this
  • Include string.h in socket.cc
  • Include string.h in udpsocket.cc
  • Include string.h in tcpsocket.cc
  • Include string.h and stddef.h in localsocket.cc
  • Include string.h in netsocket.cc
After all, we you type make again, GCC compiles and produces binary outputs. Then type

sudo make install


if you want to install. Happycoders socket library is a static library by default and your application will not depent it externally. Have fun!

No comments:

Post a Comment

Thanks