Friday, June 14, 2019

C/C++ Web Development Framework (Hello World Beginner's Guide)

I have been searching for a while on how to develop a website by using C/C++ in internet and I know it sounds strange. Why do we need C/C++ to develop a website application or to develop REST APIs... It seems very weird.

In this article, I’ll explain how you can use C/C++ to develop a website and I'll use Crow C/C++ framework to create an example of Hello World.


Why C/C++ 
C/C++ is an extremely advanced and flexible language. Its performance is still unmatched by other languages. C/C++ is able to handle extremely high loads and its tight management of resources, such as memory allocation. And indeed, although doing explicit memory handling properly can be a nightmare for an inexperienced developer, by using modern programming paradigms, it can be exploited to give a powerful and safe control over resource allocation and destruction. The predictable nature of C/C++ memory management, as opposed to concurrently run garbage collectors, is an important feature when squeezing every single flop from your CPU.


Environment Specification
- I am using Ubuntu 16.04 LTS
- Download Crow C++ Framework here
- C++ compiler with good C++11 support (tested with g++>=4.8)
- boost library
- CMake for build examples
- Linking with tcmalloc/jemalloc is recommended for speed


Building Example

Installing dependencies:
sudo apt-get install build-essential libboost-all-dev libtcmalloc-minimal4 && 
sudo ln -s /usr/lib/libtcmalloc_minimal.so.4 /usr/lib/libtcmalloc_minimal.so
 
 
Then create folder
mkdir build
cd build
cmake ..
make
 
 
Folder structures 
 

Run example helloworld



Result in browser
http://localhost:18080





Sample code:
#include "crow.h"

int main()
{
    crow::SimpleApp app;

    CROW_ROUTE(app, "/")([](){
        return "Hello world";
    });

    app.port(18080).multithreaded().run();
}

Here is some of C/C++ framework that I think very useful:
- Crow C++
- Kore C
- Pistache C++
- Silicon Framework C++
- CPPCMS Framework
- TreeFrogFramework C++
- wt C++
- feather C++
- C Server Page CSP C/C++
- Cheerp C++

Thank you