Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question

marked as duplicate by MichaelT, gnat, Bart van Ingen Schenau, GlenH7, Dan Pichelman Oct 7 '13 at 19:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

yes yes it is, one of the ways is with cgi (which doesn't need to have perl on the back end) –  ratchet freak Oct 4 '13 at 14:52
@MathewFoscarini - C# isn't C++. –  Ramhound Oct 4 '13 at 15:13
@MathewFoscarini the only thing that C# shares with C and C++ is the first character of the name of the language and some minor syntax choices. –  MichaelT Oct 4 '13 at 15:14
@MathewFoscarini: Well I guess our sarcasm detectors just aren't working very well - it's Friday after all, and raining too. :P –  FrustratedWithFormsDesigner Oct 4 '13 at 15:42

3 Answers 3

up vote 6 down vote accepted

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).

share|improve this answer
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 Oct 4 '13 at 19:12
@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. –  MichaelT Oct 4 '13 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. –  Laurent Bourgault-Roy Oct 4 '13 at 19:36

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?

share|improve this answer

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!)

share|improve this answer

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