The hard part about this example is doing anything demonstrable with C++. So we'll do something very silly in just printing our HTTP output into the log file. For the kcgi inclusion, we'll need all the usual header files.
Next, we'll need our C++ bits. This is make-work, but serves to illustrate…
Now let's just jump directly into our main function.
It will do nothing more than print
Hello, world! to the browser.
But it will also emit
Said hello! into the error log.
See your web server configuration for where this will appear.
It's usually in the error.log file.
On OpenBSD's default server, it's often in
Nothing to it—looks like any of our C examples.
The difference is that we've used some C++ code to emit
Said hello! to the error log.
The next part is how we can compile this code.
For that, we'll need to use a C++ compiler instead of the C compiler we've been using to date.
(and noting again the
-static, which we need by being in our file-system jail)
Now you can install your compiled CGI script just like any CGI script.
See Getting Started with CGI in C for these steps in detail.
All of these steps work for FastCGI, of course.
(On non-OpenBSD systems, you'll probably need to use
sudo instead of