libwebsockets: Compiling libraries and projects

I think libwesockets is the only pretty much easy to use websocket in written in pure C (that can be used as web server as well by the way). The only disadvantage is that its compilation and usage is not really straight forward. Although there is an official guide how to compile the library, well... its not completely painless and that's why I'll try to summarize it here:

Instalations steps are described in INSTALL file, however some important configuration is described only in README-test-server.

By default, configure script uses /usr/local/ prefix for paths, but you can use --prefix=/usr to install libraries and header files wherever you want.

Compiling libraries

1. Configure

Put simply on Unix:


When I tried it on Ubuntu Server it was expecting all dynamically linked libraries in /usr/lib so it's probably better to use --prefix=/usr or eventually make symbolic links from /usr/local/lib to /usr/lib.

ln -s /usr/local/lib/ /usr/lib/libwebsockets

There are probably some issues on various distributions like Fedora for example so read the official guide for more information.

On OS X:

./configure CC="gcc -arch i386 -arch x86_64" CXX="g++ -arch i386 -arch x86_64" CPP="gcc -E" CXXCPP="g++ -E" --enable-nofork

The most important thing is --enable-nofork parameter because it forces libwebsockets not to use fork(). Without it compilation would fail when looking for some header files.

This configures make files to build one universal .dylib for both 32 and 64-bit architectures, but I dont think it's any useful to build also the 32-bit version because I don't know why would you compile your application to 32-bit binary on 64-bit system. Of course, I assume you have 64-bit processor, 32-bit Macs are nowadays quiet edge cases. So, I'll trim this command to build only for 64-bit architecture:

Optionally, I recommend to set --prefix=/opt/local because if you're adding dependencies on libraries or adding header files to your project /usr is invisible by default but /opt is not.

So the configuration might look like this:

./configure CC="gcc -arch x86_64" CXX="g++ -arch x86_64" CPP="gcc -E" CXXCPP="g++ -E" --enable-nofork --prefix=/opt/local

You can try to run make now. Maybe it will throw some errors like these:

mv -f .deps/libwebsockets_la-libwebsockets.Tpo .deps/libwebsockets_la-libwebsockets.Plo
mv: rename .deps/libwebsockets_la-libwebsockets.Tpo to .deps/libwebsockets_la-libwebsockets.Plo: No such file or directory
make[2]: *** [libwebsockets_la-libwebsockets.lo] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

In that case you have to use autoreconf which is not standard part of OS X and you have to install it via MacPorts or Homebrew.
Then just run:

autoreconf -i

Now when you run make it should be fine.

2. Make

The rest is the same for both OS X and Unix:

make clean
sudo make install

If you don't know where it installed libraries and header files try locate libwebsockets and you should see somewhere in the list /usr/local/include/libwebsockets.h and /usr/local/lib/ or /usr/local/lib/libwebsockets.dylib, depending on your system (/usr/local is default prefix directory).

Compiling projects

GCC in terminal

When compiling using gcc it's quiet simple just, don't forget to include compiled .so or *.dylib at the of the source files to be compiled.
Also depending on your configuration make sure your libwebsockets.h is in a directory where your gcc looks for header files (run `gcc -print-prog-name=cc1` -v (with quotes))

gcc [parameters] -I/path/to/more/headers [source files] /path/to/libwebsockets.(so|dylib)


If you're using Eclipse in OS X make sure you have correct binary parser because by default Eclipse has preselected Elf parser which is fine on Linux, but OS X uses Mach-O 64 Parser.

I don't know if I can configure a project in Eclipse to automatically copy resources like in Xcode. If you do, just leave me a comment, please.


To compile project you have to tell the compiler which libraries you're using. That means, first locate your header files. If you don't know where it is, use locate libwebsockets.h and if it isn't in a directory where your compiler looks for them by default add the path in your project settings like on this image

The second thing you have to do is to tell the compiler to compile your source codes with (or .dylib). In Xcode you can simply drag & drop libraries to projects settings or press the plus button in the bottom left corner

Eventually, you can also configure your project to automatically copy some resources you're going to use with your project (again drag & drop).

blog comments powered by Disqus