31

I am proficient in C, and I am learning C++ right now. I always played with websites (HTML/CSS), and I was wondering if it would be viable/practical to create some simple web apps using C and/or C++.

For C it would be via CGI scripts, as explained in this tutorial for example -> http://www.cs.tut.fi/~jkorpela/forms/cgic.html

For C++ it could be via a web toolkit like Wt -> http://www.webtoolkit.eu/wt

Notice that I know it's possible (as explained in this previous question -> Can C++ be used as a server-side web development language? ).

What I am asking is if it's viable/practical (i.e., not a nightmare). I am not keen on picking up another language yet as I just started with C++, but if it would make web dev incredibly easier I will consider it (or I'll postpone my web dev projects until I am ready to pick up a new language).

Also, in case you think it's viable/practical, recommendations on the route to follow would be highly appreciated (i.e., forget C and go with C++, what framework to use and so on).

10
  • Related: stackoverflow.com/questions/417816/…
    – dan04
    Jan 28, 2012 at 18:23
  • 6
    Depends how much of a C++ god you are.
    – Raynos
    Jan 28, 2012 at 19:29
  • There's an entire web server, G-WAN, written in C and using C as it's "scripting language". Looks interesting to me, and probably something you want to check out. Jan 28, 2012 at 19:41
  • 3
    I suggest to take a look on CppCMS.
    – Artyom
    Jan 28, 2012 at 19:51
  • 1
    Alot of people really hate on PHP, but remember you can write bad code in any language. It's possible to write really good PHP code and it's really easy to write really horrible PHP code. A lot of the hate comes from language evangelists that I compare to wine snobs.
    – William
    Apr 27, 2012 at 18:16

8 Answers 8

31

C++ as a web server language is a good idea under some constraints only. From the CPPCMS website :

When CppCMS Should Be Used.

C++ language is far from being popular for Web development for many reasons: lack of appropriate tools, skills of developers and many more.

However, there are areas where C++ web programming with CppCMS becomes very useful and efficient, and some where it is just a waste of time.

When CppCMS should or can be used?

  1. High load web sites and application with hundreds and thousands hits per second, where high performance, efficiency and scalability is required.
  2. Application that require scalable Comet/Server Push1 technologies --- CppCMS can efficiently handle hundreds and thousands simultaneous HTTP connection with minimal resources usage.
  3. Embedding web interface2 into existing C++ applications/services with a small cost of additional library.
  4. Embedded underpowered devices -- CppCMS allows creation of rich applications with relatively low cost of hardware that would perform reasonably fast.

When Not To Use?

If you create small web applications that do not require high loads and require very short time-to-market period -- probably tools like Django or RoR would be more appropriate for such tasks.

Also, take a look at the rational for the need of CPPCMS.

If you build your personal blog, create small or even medium community or building a web site for a small company --- CppCMS is not for you. Take any of existing and good CMS like Drupal or develop with great Django framework, you'll be fine.

However, when the loads become more then average, the process of scale-up using existing web frameworks may be painful:

Low performance of dynamic or JIT languages enforces you to add more servers even on quite small loads. The caching becomes more complicated and less efficient because the system becomes distributed and does not scale-up linearly. Creation of such system requires skilled stuff and costs even more. CppCMS allows you to increase the performance of typical system by an order of magnitude and thus:

Remove requirement of maintaining a big server farm where few servers or even single one would solve the load problems. Reduce maintenance costs and power consumption.

Now, if you think you'll make this kind of website, it might be of interest.

Note that most devs will not need such power. If you want to do something that might really be power consuming, and in a way that is obvious, not because you pessimize, then why not use C++.

10
  • 21
    If you cant learn another language you may have more limitations than you think...
    – Rig
    Jan 28, 2012 at 20:12
  • 3
    Learning enough Python to build a website is insanely short time (use Django as framework). Learning enough Ruby is almost as short. For more performant programs you could learn C# or Java. Anyway, all those languages are quick to learn, really. Also, don't mix C and C++, it's not the same language, no really experimented C++ users would make that mistake ;)
    – Klaim
    Jan 28, 2012 at 21:01
  • 3
    Herb Sutter in his "why c++" talk said that 88% of datacentre costs is hardware and power. Electricity prices today, I can believe that!
    – gbjbaanb
    Jan 29, 2012 at 15:45
  • 2
    If you know c++, then it will take you a day to learn JAVA, and half a day to learn C# after that. Learning the accompanying frameworks... that is a different story. Anyways, if you like c++ so much, I would recommend JSP or ASP.NET. They would probably not take you that long to learn considering your skill with c++. Feb 4, 2012 at 6:33
  • 7
    @AndresF. Actually, recent interviews involving Alexandrescu that works at Facebook suggest that more and more code of Facebook is actually written directly in C++. Modern C++ (in particular if you can use C++11 compiler) is far less a productivity problem than you think. Also, you seem to think that "it isn't worth" because it is less productive than languages where productivity of the developer is the target goal. You are certainly loosing perspective as the projects we are talking about, life Facebook, really rely on efficiency of the software, not of the developer. That's their bottleneck.
    – Klaim
    Apr 27, 2012 at 13:30
28

Today C++ web development is as easy as web development with other languages that are traditional for this field (Java, C#, Ruby, PHP, Python).

CppCMS gives you all you need to do web development in C++.

So if C++ is the language of your choice... There is no reason not to use C++.

However you will still need to learn many web development concepts: HTML, Ajax, Forms, MVC, SQL, Session management and so on.

Disclosure: I'm the author of CppCMS.

2
  • 7
    Nice work of CPPCMS. Though, I am from Telecom industry, y'day I was simply seeing the website. It's a good tool for C++ devs.
    – iammilind
    Jan 30, 2012 at 1:49
  • 2
    Thank you for creating CppCMS. This fits my needs perfectly and I'll be using this for my very first website :)
    – quixver
    Aug 21, 2013 at 23:07
18

I've done web development in C++, and I'm convinced that it's the wrong tool for the job. Mostly because it's a "batteries not included" language, where those "batteries" are things like:

  • Unicode support
  • Parsers/builders for common data formats:
    • XML
    • JSON
    • MIME messages
  • a SQL database client

which are present in the Python standard library or the .NET Framework.

You can get this functionality in C++ with third-party libraries like POCO, but it's still less convenient than having it standard.

6
  • 5
    I started with c and c++ and I whole-heartedly agree with this answer. You can pick up just about any other modern language and it will be a wildly better experience. Python, .NET, Java, Ruby on Rails, Groovy/Grails, clojure+ring+compojure,and probably 10 others I can't think of right now. Seriously. Anything and everything is a better fit for web development.
    – Kevin
    Jan 28, 2012 at 19:42
  • 2
    See, that is why frameworks like C++ help as out of all the points you noted... it provides all of them :-)
    – Artyom
    Jan 28, 2012 at 19:50
  • 2
    Have a look at Boost library.
    – user4626
    Jan 28, 2012 at 21:11
  • 12
    All C++ compilers support unicode. All of them have DB clients (unless you mean embedded ones like sqlite?), XML libs are 10 a penny (I prefer tinyXML myself) and JSON (JSONSpirit is reportedly the 'best') and MIME (mimetic is good). True, I'd like to see these added to Boost for a one-stop-shop, but the point of C++ is that a lot of libraries come from anywhere, so you can get better ones rather than be stuck with whatever your framework gives you, which matters when new stuff comes out and your framework then has to reply on 3rd party libs too.
    – gbjbaanb
    Jan 29, 2012 at 15:39
  • 4
    also, when I last tried to get ReviewBoard running, I was fetching eggs all over the place, compiling them and finding issues with versions (why has Python got 3 major versions!). The problem is just as much an issue with Python as with C++.
    – gbjbaanb
    Jan 29, 2012 at 15:42
11

Yes C++ can be used. I would recommend using a framework like Lacewing to simplify things.

There are two good reason to use C++ for web development

  • You need a HTTP server on a embedded device
  • You need speed, real speed. Your either scaling into the millions or are doing CPU bound computation (think dynamic image processing or heavy AI)
11
  • 1
    speed is different than scaling. You can do horizontal scaling with almost any language or platform. C or C++ will be appropriate in only the most exceptional cases.
    – Kevin
    Jan 28, 2012 at 19:45
  • 1
    @Raynos: dynamic image processing or heavy AI is not related to web development. You could do the web app in Python or PHP and the heavy number crunching in C.
    – orlp
    Jan 28, 2012 at 20:21
  • 1
    Or you just use a non-blocking C++ IO library like libuv. There's no reason to use .NET when I can just do it in C++. At least C++ is portable.
    – Raynos
    Jan 28, 2012 at 21:11
  • 1
    @Kevin: the trouble with horizontal scaling is that its not 100% efficient, sooner or later you'll run into problems. Also, if you write your server code in an inefficient system you'll have to buy more servers - which today costs a lot in electricity alone. This is why MS is pushing C++ again now cloud computing is cool and needs much more resources.
    – gbjbaanb
    Jan 29, 2012 at 15:30
  • 1
    Language choice is usually a trade off of performance vs time to market (and also arguably maintainability). Startups usually skew towards time to market.
    – Kevin
    Jan 29, 2012 at 17:53
11

For server-side applications, then the answer is absolutely yes. Many large sites use C++ simply to provide the performance and resource usage that scripting languages don't. Many years ago a lot of web server code was written in C++ anyway (see things like Microsoft's ISAPI or ATLServer frameworks) or just as CGI applications. This changed when the web became more popular and needed faster development turnround. A script language is always going to be faster to develop, especially when you're just 'knocking something together', and was always very popular with junior developers getting in to programming.

As proof look to the most popular language for web development, PHP. Its a fine environment in which to work but no-one will say its the best language in the world.

Today, I'd hope that things have matured to a point where the server-side backend should be written with as much professional care as any other application. That means C++ can make a comeback in web development (and I think MS is going that way, as Herb Sutter reported that C++ will reduce the energy requirements in the data centre, which with huge datacentres we have today means a significant cost saving. Hence the C++ renaissance at MS, though I'm not convinced that'll turn into anything beyond system software for them)

One thing to note though: it is trivial to write C++ backend software that sit as services that are called from simple scripts on the web server. I did this with my old place of work - the web server was there solely to provide a pass-through to the application services. This allowed us to provide much better security (anyone hacking the exposed webserver got very little to work on), much better performance (as the business logic was distributed on servers that didn't web-serve), and they were very easy to test - the back-end services were stand-alone and provided explicit and well-defined interfaces so they could be tested without the web part of the total infrastructure.

There are some interesting developments in C++ as web server client code too. I saw Wt recently. This allows you to write Qt code but have the GUI display in the browser - the gadgets displayed on the browser are just thin javascript/html/etc that transparently call into the Qt/C++ code running on the server. Have a go with their Hangman example (though they get 0/10 for the graphics, typical developers :) )

I think this is a natural evolution of RPC mechanisms, where once we could run an object locally or remotely without (as a developer) caring where the object was located, now we can disconnect GUI from objects in a web browser rather than a desktop app.

5
  • +1 for the good C++ insight and the note about a modular architecture which implements a C++ backend. Can you go into more detail about how you implemented the web server pass-through to the C++ backend? What technology were you running on the web server? How did it communicate with the C++ backend (application server)? That sounds like a great setup to me (with or without C++ included).
    – nairware
    Jun 8, 2014 at 4:27
  • 1
    comment's too short for detail but ... its simple: any traditional web server will do, but instead of accessing a DB, you put all that logic in a entirely separate service (no a different box usually) that in turn accesses the DB. The service and web can be written in anything, though I have found putting an RPC interface on existing code works wonderfully. In the last case it was PHP web talking to C#/WCF service using SOAP1.1 (I wouldn't use this again - WCF is too bloated, there are cheaper comms available). Apart from security it also is the way to scale - add more web or app as reqd.
    – gbjbaanb
    Jun 8, 2014 at 11:14
  • @gbjbaanb So, for a web app where I have to constantly invoke shell commands, like compiling Java files given by user, server-side C/C++ would be the the best choice? Or is the hit in performance by higher-level languages not significant enough to forgo the simplicity in development? Jan 28, 2016 at 21:51
  • @Abdul depends on what you want to do, or how much processing is done in the service compared to the data IO getting
    – gbjbaanb
    Jan 28, 2016 at 23:12
  • @gbjbaanb the app takes a file of some extension, like .java, compiles it, and runs it against a test program, and then spits the results back in in the front. So the calls in this scenario would be 1. The compilation 2. Running the test program. That would be the main functionality. Jan 28, 2016 at 23:15
1

I can recommend the mongoose library as a web interface for C based programs, I have used it multiple times and it's very easy to use.

Though maybe PHP is a better option for web development than C or C++. It's easy to learn once you know C and C++ (the syntax is very similar). You can save more effort with learning PHP than struggling with C and C++ for web development . . .

1

IIRC eBay is built upon ISAPI which was written in C++ using the Microsoft Active Template Library server modules.

It's possible they made an early bet on it but now have no compelling business reason to move off it. Indeed, Facebook is compiled into a single large C++ binary but they develop in PHP and use HipHop compiler to transliterate:

HipHop cross-compiler: https://developers.facebook.com/blog/post/2010/02/02/hiphop-for-php--move-fast/

Single binary: http://en.wikipedia.org/wiki/HipHop_for_PHP#HPHPc

2
  • Nope -- Facebook tried the compile to C++, but it added complication without much improvement in performance. They now use a custom JIT compiler backend on the PHP interpreter. May 13, 2013 at 7:47
  • I've added a link to give more details. May 16, 2013 at 8:08
-2

For webpages, yes your HTML and CSS are good (now even better with HTML5 and CSS3). For web apps probably Flash and HTML5.

What I'm saying is that these are the easiest and fastest ways due to available resources compared to C and C++ unless you are a hardcore C++ enthusiast or if you're trying to accomplish something big!

1
  • 3
    These are client side technologies - the OP is talking about server side programming using C++
    – MattDavey
    Apr 27, 2012 at 16:18

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