I am writing a multiplatform program in c++ that should run on AMD and Raspberry Pi 4.
On AMD I installed cpp-httplib-dev
like this: sudo apt-get install libcpp-httplib-dev
and then the code like below works just fine.
#include <httplib.h>
...
httplib::Server svr;
svr.Get("/hi", [](const httplib::Request &, httplib::Response &res)
{ res.set_content("Hello", "text/plain");});
std::thread _thread( [&](){svr.listen("0.0.0.0", 8080);} );
But I cannot install such a library on Raspberry Pi.
I was looking for something similar to apt search
with no results for C++.
Question: Is it possible to create a simple HTTP server on Raspberry Pi with just C++? If yes, how?
If not, what are possible solutions?
NOTE: My project is quite big and must be in C++. Eventually, I can have the server part written in another compilable language, but how then pass information between those programs?
(Then just need to pass simple strings between programs, like: "R1 Down"
, "R4 R5 R8 Up"
)
1 Answer 1
I've never used httplib before, but a quick check on an x86-64 machine reveals an important point:
cpp-httplib-devel.x86_64 : A C++11 single-file header-only cross platform HTTP/HTTPS library
^^^^^^^^^^^^^^^^^^^^^^^
If you are new enough to C++ that you don't know what a "header only" library is, you may want to look that up, but in short this means the library is a single .h/.hpp file which only requires you #include
the header in order to use it -- there is no separate pre-existing compiled object that needs to be linked in. Hence, there is not actually a set of binary packages, each targeting a different architecture. There is only the header file, which is textual C++ code.
Why it then isn't available in Raspbian I don't know, but since it is apparently also cross-platform, all you need to do to use it on the Pi is copy the one file over and you can attempt to compile your project with that without doing anything else.
Is it possible to create a simple HTTP server on Raspberry Pi with just C++?
Even without any http library, yes, but you would have to write the whole thing yourself, which for a complete general purpose server would be quite a bit of work.
apt show libcpp-httplib-dev
suggests the package does exist - what version of Raspbbery Pi OS have you got installed?