C++? Are You Crazy?

Cool projects are using this big, complicated language. Maybe you should too.

About two years ago, programmer Dan Egnor posted to advogato.org with the question, “Why don't C++ and free software mix?” He pointed out that freedom-loving software developers tend to stay away from C++.

But, he added, although C++ is a big complicated language with “terrible pitfalls and simple misfeatures”, it is standardized and offers good flexibility and performance. And, he wrote, “its standard library includes the STL, which knocks the socks off anything available in the C world for power, flexibility and efficiency.”

Or, does C++, as many have argued, represent the worst of both worlds, an infertile middle ground between the simplicity and control of C and the almost-automatic everything of Perl and Python?

Today, though, it might be time for a second look at this much-maligned language. For two big reasons books on standard C++, templates and all, are on my to-read stack above the more tempting ones on the next great scripting languages. First, the tools are good. The C++ support in the GNU Compiler Collection (GCC) is being actively cleaned up, with binaries getting smaller and version 3.2 offering a stable application binary interface (ABI) that will help with deploying software written in C++.

Besides GCC, a lot of other good tools are available to help with C++, as Cal Erickson points out on page 34. Cal does embedded development and that article is in the Embedded section, but his development strategy—get as much as possible working on your workstation first—means that the article will be useful to any C++ programmer.

Second, new or newly freed C++ projects, such as the Xerces XML parser contributed by IBM, mean that your new C++ code can draw on a lot of already tested, supported functionality. See John Dubchak's article on page 50 for an example. As more corporations start sharing in-house code, corporate technical preferences such as C++ start to be more important on the outside.

If you're looking for a place to apply your software development skills, Len Kaplan has a great one—your local museum. On page 89 he covers the unique challenges and rewards of creating applications for museum exhibits. And, you might pick up some C++ and XML hints from that article, too.

Another school of thought favors doing object-oriented programming in C. For an example of how that is happening in the kernel, see Greg Kroah-Hartman's Driving Me Nuts column on page 28. Your brain is inside your head, so people won't see the stretch marks on it from reading his code.

But speaking of brains, don't worry. We couldn't let the development issue slip by without at least one regular C article, and you'll be happy to know that the performance-critical parts of interpreting the brain waves of the test subject on the cover are in C. Enjoy Sam Clanton's Matlab-to-C porting advice on page 56.

Don Marti is editor in chief of Linux Journal.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: From the Editor, June 2003: C++? Are You Crazy?

Anonymous's picture

Object-orientation is not necessary in many projects. If it is, OOP in C is fine, or Objective-C, which doesn't require a C programmer to have a nervous breakdown. And, if it can be done in Perl or Python (or at least prototyped), eveb better. I personally believe that a large project can be best done in ANS Forth, which allows FULL control, and perfect encapsulation of ones ideas in 'words', composed of other layers of words. (This is not going to go over well, I'm sure, but you won't change my mind.)

Re: From the Editor, June 2003: C++? Are You Crazy?

Anonymous's picture

I have been a s/w developer since 1976, done years of C/C++ devo work, & taught C++ as an adjunct prof at a local Univ. I have been totally committed to Java since 1996. My experience is that when most companies want a C++ dev'er they really mean plan old C (but may use a C++ compiler - they don't know the diff). There are many great langs available for dev'ers, but even thought the C++ STD is available and solid you can't beat the number of classes available (for free) from the J2SDK. Doug@javadoug.com

Re: From the Editor, June 2003: C++? Are You Crazy?

Anonymous's picture

I still like C since most of people(hackers) have written codes in C rather than C++. If people like OOP, why don't they use a different language such as Java which is used for web development as well.

Re: From the Editor, June 2003: C++? Are You Crazy?

Anonymous's picture
  • Java is really slow... by essence (semi interpreted), and also suffers from too much control from SUN.
  • c++ is powerful, and complex, somewhat clumbsy too,
    But has excellent libraries, and debuggers etc...
  • But Bertrand Meyer's Eiffel is quite appealing (clean, simpler), but what we would need is a good GNU Eiffel environment, which does not exist yet.

    I mean a good compiler, debugger, profiler, and of course good libraries.
    So far only mostly proprietary Eiffel exist, though there is a GNU small Eiffel, but the code generated is C, and unless I am mistaking there is no debugger.

    Andre G-

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    In my opinion, the main justification for C++ is that tools such as Rational Rose RT generates C++ code as the translation vehicle for a model (i.e. design) ;-)

    But - compared to RoseRT - actually *coding* in C++ feels a bit like printing a book by carving the individual types for each page out of little blocks of hardwood and mounting them in frames for an old Guthenberg-style printing press would compared to using a DTP tool.

    Unfortunately - RoseRT et. al. is ridiculously expensive and there is no way that an open-source project can use exclusive/proprietory tools because that will lock out the large majority of potential contributers.

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    Personally, I learned C++ after minimal exposure to C, but I have worked on several other languages with varying OO expressive power. I find that object oriented design seems to always eventually "come out" of whatever large project you happen to be working on anyway. You end up using structs and functions to act on the structs... but in the end it's much better to simply design a class. For one thing, you can build in error checking and try to best to guarantee that this part of the code will transparently integrate with other parts in a 100% predictable manner, and for another you can test it independently of anything else with much fewer inputs meaning it takes less time to test all possibilities.

    Classes (or structs) can be annoying if you're entering your code into a text editor and don't realize you misspelled something until you try to compile, but they're absolutely wonderful if you're editor will pop up a list of member functions the moment you type a dot or arrow after a class variable/pointer and complete the text for you.

    I agree, though, that C++ is a big language... I know quite a bit of it, but I still feel like there is much more to learn. I always wonder how much harder it must be for a non-English speaker to learn to program in these languages based on English words, abbreviations, and acronyms, especially since a good portion of the Open Source community does not reside in English-Speaking countries.

    But in closing, I'd have to say that on today's machines, any performance complaints about C++ are pretty insignificant, especially since you can resort to C-style coding and optimization techniques in any mission-critical code areas. Of any OO language meant primarily for compiling stand-alone binaries, though, I can't think of any more widely supported and understood than C++.


    C++ is nasty in comparison

    Anonymous's picture

    to Objective-C, which unfortunately wasn't taken up for a whole variety of reasons. Objective-C has cleaner syntax and in my opinion is much easier to learn for C programmers than C++. Hopefully OS-X and Cocoa will increase its popularity. If you're familiar with C check it out.

    It's got dynamic typing, smalltalk like messaging syntax, cleaner interface declaration etc. etc. gcc -lobjc


    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    C++ is almost always the better choice than C. Simply put it is a more powerful language. More powerful languages allow you to express yourself better because they offer constructs absent in simpler languages.

    High level languages are almost always too slow for applications that I tend to write (computational geometry). That leaves me with a choice of Fortran or C or one of its descendants (java excepted).

    Between coding a couple of million lines of code in C and C++ I'd take C++ any day. It may not be the purest OO language but the encapsulation it offers can greatly help manage complexity. STL is the most powerful container library written.

    The language is tough to learn but a lot of its less pleasant features stems from performance concerns. C++ was designed so that every feature would come at a minimal expense in terms of runtime overhead and Stroustrup insists on keeping the language that way. This may delegate C++ to a niche but it is still a very important niche to handle.

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    After programming in C++, C and Perl I am discovering LISP, which using a good compiler is more than fast enough (close to C and much faster than java) but very high level (closures, garbage collection, typed values but untyped variables, advanced OO system, powerfull macro language, multiple function return values, functions as data, etc....) and extremely adaptable to different programming styles.

    It took my two tries to get to grips with the language (first time the brackets scared me off), but now I would dread to go back to a complex underperforming language like C++.

    Taking your argument that a more powerful language is better, LISP is the more appropriate choice.

    (by the way cmucl is about as fast as fortran on numerical computations).

    Re: From the Editor, June 2003: C++? Are You Crazy?

    synec's picture

    why don't free software developers do more projects in c++? simply put, free software developers usually get to pick what language they write in; hired guns usually don't. programmers working for a company are paid to put up with c++.

    so why do free software developers often choose other languages? most immediately, free software developers are focused on what the program is supposed to do. all programmers have to think about code design, but why subject yourself to dealing with implementation details that have nothing to do with the behavior of your program? should the return type of this function be const? when do i choose private inheritance over encapsulation? is this code exception-safe? (this last question is much harder to answer in c++ than other languages with exceptions. read items 8 through 19 of sutter's exceptional c++.)

    often free software developers are not being paid for their efforts, and for so many projects, another language will work just as well without the incredible hassle of writing in c++.

    yes, the stl is a good thing. being able to write code that is type independent is powerful -- just ask any python or perl programmer. of course, generic programming in statically typed languages is going to be harder than in a dynamic language like python. the next version of java will (finally) have generics too, and it looks like the syntax will be much cleaner there. again, using the stl is far from intuitive: the remove algorithm doesn't remove anything from a container, creating containers of auto_ptrs won't work, equality is not the same as equivalence, and you should call remove on lists but erase on others.

    you learn other languages. you study c++.

    "First we crawl. Later we crawl on broken glass."
    --Scott Meyers, Effective STL

    still working non *typesafe* languages

    goon's picture

    why don't free software developers do more projects in c++
    Choose when and when not to use *non* type safe languates. Beacause I dont always need the power of typed languages and choose a language with memory managment. Now I don't mean you do not need your fundamental *knowledge* of low level concepts like pointers. You simply get more done by choosing when and where you dip into the c or cpp low gear.
    The counter technical argument I hear a lot is *but you need* the power of c/cpp when using gui programming - what about separating the gui from the business logic?

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    Though C++ is a bit complicated and has a somewhat "dirty" OO design (Java is quite clean), it has proven itself in OpenSource development, and can be quite powerful. KDE is a perfect example of that. A number of other opensource GUI environments also use C++, like wxWindows

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    I do not intend to start a language flame here ( I believe every
    language has it's own merits and the choice of an implementation language should be based on problem at hand ) but it is really
    strange to see C++ being ignored for open source development
    especially when

    + the language is around for almost 20 years!
    + it is an ANSI standard language
    + Being a superset of C, it allows you to leaverage all your
    existing / legacy code in C.

    In addition, there are several features that help you in writing
    better software

    + constructors/destructors
    + encapsulation through objects
    + inheritance
    + STL is a brilliant piece of work and it's quite feature rich

    I'm sure, you can add many more things to the list.

    BTW, thanks for pointing out that GNU tools offer excellent C++ support and they are being improved all the time.

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    A few good reasons:

    * Compiler and library support had been poor and non-standard for years. This has had a lingering effect on the reputation of the language in the free software world.

    * It's a big, complex, downright intimidating language, and many people just haven't had the time or inclination to navigate through the potentially treacherous waters.

    * the old "C plus classes" paradigm was not very compelling, yet this is still the approach of many texts on the market (see www.accu.org). The more STL driven approach has taken time to trickle down.

    I do think things are improving, though, with hugely better compiler and library support, and much better books available (like _Accelerated C++_ and various STL tutorials).

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    I could think of three reasons why C++ isn't popular in the open source world. Firstly, compiler support for the full language (exceptions, templates) hasn't been around for too many years. The same is probably true for utilities like libtool. Secondly, until recently, you couldn't mix object files built by different versions of the compiler. That's a problem if you built a C++ shared library, upgraded your compiler and then tried to use the shared library - or if you're doing binary distributions. Finally, C++ is a large language with many powerful features. Like any powerful tool, it takes a fair amount of effort to understand how to use it effectively. It is simply much easier for people to learn C than C++.

    - Andrew Birkett

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    Stepping around the various issues that people have already highlighted with the use of C++ runtime features in operating system kernels, I am not surprised that C++ hasn't been more widely adopted. It is arguably only the past couple of years that reliable standards compliant C++ compilers have become ubiquitous - "arguably" because C++ is one of those languages whose implementations always invite deviations from the standards-prescribed behaviour, if only by accident.

    Meanwhile, higher-level programming languages have very much "stolen the clothes" of C++: Java may have been seen as a "dumbed down" C++, but it delivered the goods that interest developers the most (garbage collection, threading, extensive standard libraries); Python and other languages (such as Ruby) give you an even higher level of abstraction, garbage collection, threading, extensive and *useful* standard libraries (see the pattern?), with the possibility to integrate with C and C++ components natively. Whilst C++ has STL, and it may be efficiently and beautifully implemented, the developer gets to see code which isn't so beautiful, especially when their compiler starts to complain about...

    some<strange_ &problem, with_<their *__templates> *they, did<not **even_know__, they &were> *_using_>

    And I think you'll find that most object-oriented languages offer the wonders of constructors, destructors, encapsulation and inheritance. Reaching for C++ every time an application needs developing is like reaching for a heavy, slippery hammer from the toolbox every time some carpentry needs doing.

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    even as easy a c++ is easy to read and write I think that the
    C++ code is just find I have all ready had three class in C++ the only problem I had was understanded it at first.I have heard from mu teacher at colleage that C++ can be a pain for
    rewriting or doing changes in linux kernel.

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    From Documentation/DocBook/kernel-hacking.tmpl:

    Using C++ in the kernel is usually a bad idea, because the
    kernel does not provide the necessary runtime environment
    and the include files are not tested for it. It is still
    possible, but not recommended. If you really want to do
    this, forget about exceptions at least.

    From the GNU coding standards, section 3.1:

    When you want to use a language that gets compiled and runs at
    high speed, the best language to use is C. Using another language
    is like using a non-standard feature: it will cause trouble for users. Even
    if GCC supports the other language, users may find it inconvenient to have
    to install the compiler for that other language in order to build your
    program. For example, if you write your program in C++, people will have
    to install the GNU C++ compiler in order to compile your program.

    C has one other advantage over C++ and other compiled languages: more
    people know C, so more people will find it easy to read and modify the
    program if it is written in C.

    So in general it is much better to use C, rather than the comparable

    Now my two cents: So the Linux kernel actively discourages C++ and the
    GNU folks actively discourage C++. But Mozilla is a lot of C++. It is the
    same as any other part of the programming industry: the project leaders
    choose the language. If you want to program in C++, start your own
    project or find a project that uses it.

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    When you want to use a language that gets compiled and runs at high speed, the best language to use is C.

    umm ... any connection with the fact that gcc still kind of sucks optimizing some parts of c++ code? (being clearly behind compilers like intel and portland on x86). don't get me even started on the fortran compiler ... they might as well just drop it altogether, the binaries it makes are sooooo slow. one might as well read that as 'when you want the best performance gcc can give to your program, use c ... for anything else don't blame us for wasting your wasted time (err ... or get another compiler)'.

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    i just farted

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    You could avoid that by keeping your mouth closed!

    Re: From the Editor, June 2003: C++? Are You Crazy?

    Anonymous's picture

    Farts come out of the anus, not the mouth...
    still hard to believe people don't know these things...

    Geek Guide
    The DevOps Toolbox

    Tools and Technologies for Scale and Reliability
    by Linux Journal Editor Bill Childers

    Get your free copy today

    Sponsored by IBM

    8 Signs You're Beyond Cron

    Scheduling Crontabs With an Enterprise Scheduler
    On Demand
    Moderated by Linux Journal Contributor Mike Diehl

    Sign up and watch now

    Sponsored by Skybot