I understand that as C++ isn’t dynamic, its not seen as a particularly great base to build a website, but I believe it partly falls down to support. Are there any servers that run C++ programs as like a root, for example node.js creates the server and then handles all requests that go to the root yet CGI (which I don’t want to use) is like a Php webpage, limited to slow Apache with annoying re-writes. Are there any C++ librarys that let you easily embed a server.
Share
I recently (as in two days ago) had to rewrite a Java Jetty servlet in C++ (due to some JNI issues with OpenCV which I was also using). I had the exact same question as you and there’s no easy answer (I also didn’t want to use CGI). I suggest a couple of things:
Use boost, use boost, use boost.
Boost makes cross-platform deployment absolutely trivial and (dare I say it) fun! My dev machine is running Windows 7 but I had to deploy the server on a Linux server and boost made it incredibly easy.
Use an HTTP library
For my project, I found cpp-netlib, which makes writing servlets (if you can even call them that) very very easy. It offers access to
requestandresponseobjects and even supports multithreading. Here’s the server example from their website:Very neat-looking and (from what I’ve seen) it’s also very robust. Do not (do NOT) write your own HTTP 1.x implementation. I strongly advise against this because it’s a huge time investment and there’s no reason to reinvent the wheel.
Do what works for you
If you need your server to have some hot-swappable parts, feel free to use an easy scripting language like LUA – otherwise, just be ready to compile at every iteration.