30

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

8 Answers8

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

Klaim
  • 14,902
  • 4
  • 51
  • 62
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.

fossegrim
  • 123
Artyom
  • 2,079
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.

dan04
  • 3,957
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)
Raynos
  • 8,590
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.

gbjbaanb
  • 48,749
  • 7
  • 106
  • 173
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 . . .

gregn3
  • 117
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

JBRWilkinson
  • 6,769
-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!