7

For once, I have come across a lot of stuff about the use of C++ being not advisable for SSS and recommending the use of so called interpreted languages like PERL and PHP for the same. But I need the advanced OO features and flexibility of C++ to ensure a scalable and more manageable code.

I have tried many internet articles and searches and none where helpful to the point that I still have no idea if it is possible to write SS-Scripts in C++ and if yes, then how.

I have thought of couple ideas, including writing a web-server in C++ and responding accordingly after parsing the HTTP request. But it would be re-inventing the wheel and I'll end up deviating from my main project and dedicating a lot of work to ensure a functional-cum-secure HTTP server.

I have also considered PHP extensions but again the approach also comes with its own baggage and overhead.

My questions are:

  • Is it possible to program SSS in C++?
  • If yes, then what are the approaches at my disposal.

Thanks!

check123
  • 1,937
  • 1
  • 21
  • 27
  • Why doesn't python work for you? – Benjamin Bannier Apr 18 '11 at 18:03
  • Define "server side scripting" – Brian Roach Apr 18 '11 at 18:03
  • @Brian: SSS to work with server system and database tier. – check123 Apr 18 '11 at 18:05
  • @Honk: I am barely into Python and despite being keen, time constraints me to develop my Python skills further. – check123 Apr 18 '11 at 18:07
  • That's ... really not answering the question. What is it you're trying to do that you feel the languages everyone else uses for "server side scripting" won't work for you? What do you think "server side scripting" means? (especially given c++ isn't a "scripting" language) – Brian Roach Apr 18 '11 at 18:07
  • Ok! Confess: The functionality provided by interpreted languages is fairly adequate for me. It is the class hierarchies where I find PHP far more restrictive than C++. – check123 Apr 18 '11 at 18:16
  • @check Actually, it is possible to write quite nice OO code in PHP. And you have other options, such as Python. But I would investigate PHP further first - it is nowhere near as bad as some know-nothings make out. –  Apr 18 '11 at 18:20
  • @unapersson: Yes, you are right about PHP, it is an excellent language for SSS but somehow it does not fit my needs. – check123 Apr 18 '11 at 18:24
  • How does it not fit your needs? Again ... if you explain what it is you're doing that no other language but c++ can solve, we might be able to help. Otherwise this question is not answerable and you're simply looking for validation. Basically, c++ is your *worst* choice, unless you have some very specific need. – Brian Roach Apr 18 '11 at 18:31
  • @Brian: I am not saying no language but C++ will suffice, .Net is another FW I can look at. And somehow I may be able to generate advanced OO functionality in other languages also, but I am low on server resources (which only is accentuated given the fact that extensive computations need to be done on the server on large datasets fetched from DB) and would like must better control over the server system including the memory. Also, every programming language has its own learning time, which again I am short of. – check123 Apr 19 '11 at 07:00
  • I am writing a web-service that allows analysis over cross-market data does a lot of computation to identify patterns and other quantitative analysis. – check123 Apr 19 '11 at 07:04

6 Answers6

8

Ignoring, for the moment, the advisability of using C++ for SSS, your first choice would probably be Wt. Contrary to the implications in some of the other answers, no development time is not likely to increase by 10x (or anywhere close to it). No, you're not missing all the nice infrastructure features you'd expect in things like PHP, Perl or Python either.

In fact, my own experience is rather the opposite: while PHP (for example) makes it pretty easy to get a web site up and running fairly quickly, producing a web site that's really stable, secure, and responsive is a whole different story. With Wt, rather the opposite seems to be the case (at least in my, admittedly limited, experience). Getting the initial site up and running will probably take a little longer -- but about as soon as it looks, acts, and feels the way you want, it's likely to need only rather minor tweaks to be ready for public use.

Getting back to the advisability question: developing in C++ may be a bit more complex than in some languages that are more common in the SSS market -- but it's still a piece of cake compared to doing security well. If somebody has even the slightest difficulty writing C++ (e.g., tracking and freeing memory when it's no longer needed), I definitely don't want them getting close to the code for my web site.

Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
  • Development time may not increase by 10x (though I wouldn't be surprised if it did), but it will increase - the C++ compile/link/install cycle is going to be significantly longer for changes to C++ code than it will be for changes to a PHP script. –  Apr 18 '11 at 19:17
  • @unapersson: You seem to be taking for granted that the compile cycle will be the *only* difference (or even a significant one). At least IME, that's not even close to correct. – Jerry Coffin Apr 18 '11 at 19:29
  • @Jerry IME the time taken for the PHP interpreter to do its stuff is significantly shorter than that for the C++ compiler and linker (let's not forget the linker) to do its stuff. But YMMV. –  Apr 18 '11 at 19:37
  • 1
    @unapersson: You're still missing the point: the vast majority of development time is designing and writing code. Compilers, interpreters, etc., make up only a tiny minority of the time. – Jerry Coffin Apr 18 '11 at 19:40
  • @Jerry You are missing the fact that I can (and have done) write well-designed, modular, OO code in PHP. There is no extra overhead for PHP vs C++ coding or designing that I have ever come across, with the exception of static type checking, which is both a plus and a minus. Have you actually written anything significant in PHP? –  Apr 18 '11 at 19:56
  • @unapersson: yes. Have you written anything significant with Wt? The significant difference here isn't really C++, but Wt. If you used (for example) WtRuby or JWt, it wouldn't change the situation drastically. – Jerry Coffin Apr 18 '11 at 20:05
  • @Jerry No, I haven't. But I don't think that the fact that I haven't used framework X prevents me from commenting on the language that X is written in, and on the effects it will have on the development process. Suppose, for example, X was written in assembler, and required me to write in assembler in order to use it :-) –  Apr 18 '11 at 20:09
  • @unapersson: You seem to be missing a number of points. First and foremost, that you *don't* have to develop in C++ to use it (I'd have thought the mention of JWt and WtRuby would have made that clear). Second, as I already tried to point out, you're ignoring major factors in favor of focusing on minutiae. Your "suppose" simply has nothing to do with reality. – Jerry Coffin Apr 18 '11 at 20:18
  • @Jerry I would point out that this question is about C++. And my "minutiae" are as important to me as your "major factors". And you seem to have missed the :-) at the end of my last comment. –  Apr 18 '11 at 20:21
  • @unapersson: yes -- he asked about C++, so I answered about C++. You then took "it can be used from C++", and appeared to interpret it as "it can *only* be used from C++" -- an outright falsehood. Yes, your "smiley" got line-wrapped so it wasn't particularly visible -- but even if I'd seen it, I don't think smiling excuses the propagation of falsehoods. As for "important factors", it sounds to me like you *haven't worked on a significant project (in any language). Even back when compiling was an overnight affair, it was still minutiae on a large project. – Jerry Coffin Apr 18 '11 at 21:00
  • You're talking about an OP that can't seem to explain exactly what it is he wants to do, and was going to write his own webserver rather than simply using apache. Do you really think any of this is applicable? You don't even know what he wants to build a web app, never mind use a framework. – Brian Roach Apr 18 '11 at 23:12
  • 1
    @unapersson: The first resort of the incompetent is violence -- or, on the internet, the Ad Hominem attack. Congratulations. – Jerry Coffin Apr 18 '11 at 23:24
  • I guess things have fairly heated up in here. Someone please switch on the Air-Conditioner. Anyways, the discussion does bring out a few interesting points and @Jerry, Wt indeed sounds quite interesting. Will look through and weigh pros and cons. – check123 Apr 19 '11 at 07:07
3

I wouldn't recommend it, but you can certainly write CGI scripts in C++ (or in C, or in FORTRAN). But why bother? Languages like PHP do a much better job more easily, and they seem to scale well for some pretty major sites.

2

CGI is the "standard" way to have C or C++ code handling web requests, but you might also look into writing a module that gets linked into the web server at runtime. Google for "apache module API" (if using Apache) or "IIS module" (if using IIS).

Kristopher Johnson
  • 76,675
  • 54
  • 235
  • 299
  • This one sounds good, I did think of trying to adopt Apache to my needs. – check123 Apr 18 '11 at 18:13
  • @check It is not good. Writing Apache modules is not at all simple, particularly in C++ - you really don't want to go there. –  Apr 18 '11 at 18:16
  • @unapersson, Would be glad if you may please throw more light. – check123 Apr 18 '11 at 18:20
  • @check Apache is pretty resolutely C. Personally, I have written all the C code (lots!) that I ever intend to. And the Apache module build/install/test workflow is pretty ghastly. –  Apr 18 '11 at 18:23
  • Hmm...But I will still like look into it if not adopt it as a final solution. – check123 Apr 18 '11 at 18:26
0

Can you afford 10x as much development time? All the infrastructure-ish bits that you take for granted in php, perl, python are non existent or much harder to use in C++. I see only two valid reasons to do this: 1. You only have C++ on your platform. 2. The server really has very high performance needs that would benefit from problem specific optimizations.

Torp
  • 7,894
  • 1
  • 18
  • 18
  • @Torp about your two points: 1. I have a fair number of technologies at my disposal. 2. If not very high, it does have higher than usual performance needs. – check123 Apr 18 '11 at 18:21
  • If you're sure about that, you might as well do a custom server. Depending on your problem, you probably won't need to write a complete http server, only a limited subset of it. Just call it 'custom server' and not 'server side scripting' :) – Torp Apr 18 '11 at 18:27
  • Yup, that sounds a pretty good idea, just that I am a bit paranoid about security and my lack of experience with server programming might leave me exposed. – check123 Apr 18 '11 at 18:29
  • @check Under no circumstances write your own server! If you think you need a custom server, take a look at lightweight alternatives such as Mongoose at http://code.google.com/p/mongoose - much easier to customise than writing your own or indeed writing an Apache module. –  Apr 18 '11 at 18:35
  • Well, from personal experience, i went that way (custom c++ code for everything) on the current project. My excuse is there was no scripting language available on my platform (custom arm running android) when i started. In hindsight, i should have postponed server development until a python port was available, or just taken the time to compile my own :) – Torp Apr 18 '11 at 18:35
0

You can write a CGI application in C++ using an appropriate framework (like this one). But I'd recommend just going with perl or php. It will save you much time. Those tools are just better suited for this kind of job.

EDIT: corrected the link

0

I couldn't understand your exact requirements (license, etc) but this might be what you are looking for http://cppcms.sourceforge.net.

Orhun
  • 324
  • 2
  • 6