8

I would like to create a web application (meaning that will run from browser). The point is that I want to write just the UI in HTML5 + CSS + JS (or maybe even some PHP). On the other hand, I want to write the functionality of the application in C or C++. Then, I want to combine the code about the UI and functionality. Therefore, I would like to ask, whether it is possible to combine C/C++ code with web markup/programming languages?

8

3 Answers 3

8

There are a couple ways of using C or C++ in the web world.

The ancient method is CGI. With this approach you have a web server that is configured to launch CGIs fork and exec the CGI when requested. The problem with this is that the fork and exec is rather heavy weight. If you have 1000 simultaneous connections, it means you have 1000 processes running - not ideal.

Moving a step closer to the web server, in particular apache this time, one could write a module for it in C which handles the code. This lives within the web server as a library and is much faster, though it means tightly integrating with the apache environment which isn't always trivial. The most recent book for this approach was published in 1999 and is likely a bit dated.

Other web programming languages and stacks have moved to an application server structure. With this approach, it is designed for hosting lightweight (rather than the heavy fork and exec) processes, often with a focus on a web facing interface for the applications.

Wikipedia lists a few C++ application servers:

  • Tuxedo - Based on the ATMI standard, is one of the original application servers.
  • Tntnet - Includes a template engine which allows embedding C++ code in HTML pages. Templates are compiled before run-time, and thus very fast. Multi-threaded, supports object lifetime via scoped variables.
  • CPPSERV - C++ servlet container.
  • Wt - A web toolkit similar to Qt permitting GUI-application-like web development with built-in Ajax abilities.
  • POCO C++ Libraries - A collection of open source class libraries including Poco.Net.HTTPServer.html
  • CppCMS

The key point with this is you likely need to work within another framework. Writing a multithreaded scalable web application is beyond the means of most programmers.

Do realize that hosting may be an issue. Nearly every web host offers php, and many offer tomcat (java). You would have to do some digging to find one that offers one of the above application servers (very few offer cgi now days and even fewer would allow for random application just to run).

One of the advantages of languages that were coupled to an application server early in their development is that the structure of the application is consistent (java has the .war file which is the same no matter what container you deploy to - unless you've written container specific code). C++ containers appear to be incompatible between each other (I welcome any corrections on this matter).

3
  • 1
    nitpicking: fork()/exec() is not heavyweight on modern systems. It's considered inefficient because of runtime startup, not because of exec(). A tight C application can be exec()'ed in less time than some scripting VMs switch contexts, especially if there are other instances running. in C++ isn't so easy... but can be done.
    – Javier
    Commented Oct 4, 2013 at 19:12
  • 1
    @Javier I'm not certain about performance... more historical background. Remember that for a CGI the web server needs to get all the information and put it in the appropriate environment variables (and if its a POST, hook up that data to standard in) and then do the fork and an exec of the process. While a tight C program may be quite zippy, that model tends to be depreciated and also has difficulty with pooling of resources (database connections, sessions, etc...) that make it suboptimal for modern web development.
    – user40980
    Commented Oct 4, 2013 at 19:28
  • About the hosting, I would point out that many host now offer virtualized hosting, which give you a nice VM to do whatever you want in it (Ruby, Python, Java, a C++ background app, whatever!). They are a more costly than shared hosting, and you need to know your way around Linux. But at 15$ per month for the low end it's not the end of the world either. Commented Oct 4, 2013 at 19:36
1

What about developing your app functionality as a separate server and communicate with your UI using REST (or SOAP)? JS can process these messages and update UI. IMHO it is a better option.

Is this solution viable to you?

0

You can use tools like emscripten too, this converts LLVM bitcode into javascript. You can combine this with clang or clang++ to run your C or C++ in the browser. (Actually on the client side, unlike CGI)

(Unreal did their html5 port this way!)

Not the answer you're looking for? Browse other questions tagged or ask your own question.