Forum Moderators: bakedjake

Message Too Old, No Replies

Running C++ programs for the web

         

zRonin

5:58 pm on Jul 20, 2007 (gmt 0)

10+ Year Member



The title says it all.. how can I run compiled C++ programs from the web? My intent is to program a website in C++ rather than PHP.

[edited by: zRonin at 5:58 pm (utc) on July 20, 2007]

phranque

6:47 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



create a wrapper script in your favorite scripting language that passes arguments and control to your c++ program.
after that, it's a matter of having your c++ program print a web document to standard output and return...

zRonin

6:51 pm on Jul 20, 2007 (gmt 0)

10+ Year Member



I don't want to load an entire language interpreter like PHP or ASP just to execute some C++. The point is that I want to reduce overhead, not that I prefer C++.

jtara

7:12 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You certainly can do server-side programming in C++ - though I'm not sure I would recommend it unless you have some good reasons.

I don't think I'd follow the "wrapper" approach mentioned earlier, but I would suggest considering writing most of your app in a scripting language, and writing portions that are resource-intensive in C++. That way, you get the best of both worlds.

The simplest way to write a pure C++ server-side app is to write a CGI app. Of course, this won't get you a great deal of efficiency, as the app will be loaded each time a page is accessed. But it's simple (CGI apps use STDIN/STDOUT and environment variables), and this could get you started. You can often convert an existing text-oriented app to CGI rather quickly.

For maximum performance, look into Fast-CGI, and/or writing your own "modules" for your web server using the web-server's API. The latter will be specific to the particular web server you use - you will have to refer to your web server's documentation.

The advantage of a module is that it is loaded when the server is loaded, and stays resident as long as the server is running. The module is passed control when certain events occur. (Such as a certain URL requires rendering.)

There are a number of libraries available to assist in CGI or module programming. Do some searching.

phranque

7:36 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you might also look into something like mod_perl (assuming apache here) which will load the interpreter and your modules once and leave them resident.

lammert

9:04 am on Jul 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The fastest execution of your code will be if you compile your application as an Apache module. In that way the module is an integral part of the Running Apache processes and the overhead when switching from Apache and your application is nearly zero.

Building an Apache module needs however significant knowledge of C, Apache and the module API.

DamonHD

10:03 am on Jul 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unless you are *really* sure of what you are doing, which I suspect that you are not because you had to ask the question, writing Web apps in C or C++ is fraught with danger and security holes, and rarely is raw execution performance the limiting factor anyway.

An entire new language (Java) was written to avoid the single largest programming problem in C/C++, which in turn has traditionally been the single largest source of security breaches on the Web, ie pointer handling and buffer overflow.

I run my Web applications as either totally static flat files under Apache (very safe and efficient) or as Java servlets under Apache Tomcat, which is (with a little care) safe and efficient also. Much faster than CGI-based applications for example.

Rgds

Damon