Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×
GNU is Not Unix Programming

GCC Moving To Use C++ Instead of C 546

An anonymous reader writes "CodeSourcery's Mark Mitchell wrote to the GCC mailing list yesterday reporting that 'the GCC Steering Committee and the FSF have approved the use of C++ in GCC itself. Of course, there's no reason for us to use C++ features just because we can. The goal is a better compiler for users, not a C++ code base for its own sake.' Still undecided is what subset of C++ to use, as many contributors are experts in C, but novices in C++; there is a call for a volunteer to develop the C++ coding standards."
This discussion has been archived. No new comments can be posted.

GCC Moving To Use C++ Instead of C

Comments Filter:
  • by Anonymous Coward

    Either that, or could we be about to see the beginning of a gcc/llvm compiler arms race?

  • Seems odd... (Score:3, Interesting)

    by man_of_mr_e ( 217855 ) on Tuesday June 01, 2010 @04:04AM (#32415956)

    I'm guessing that only the C++ compiler part will be written in C++. Sort of an Ouroborus.

    One of the reasons for gcc being in C for so many years is that it was easier to get a bootstrap c compiler running on a given platform to compile the full gcc toolchain, but I would guess that a C++ compiler was not that important to those people anyways, but it still begs the question.. how do you get a C++ compiler working on a platform that doesn't have one?

    • After a few moments of thought, the answer seems obvious, so i'm ansing my own question. They will likely have a bootstrap C++ compiler written in C that is capable of compiling the full C++ compiler.

    • Since there are platforms for which C++ compilers exist you can compile the compiler on one of these and then cross-compile for the target platform.

      This is also how you boot-strap a C-compiler on a platform it is not implemented for initially.

      • Yes, that is one way to do it, but my point was that gcc has always prided itself on being able to bootstrap itself with minimal work, and without cross compilation. Cross compilation was sort of considered to be "cheating"

        • I get your point.

          It might be possible to maintain a "gcc-light" compiler written fully in C and then have the gcc build scripts completing this boot-strap compiler first. The gcc-light do not need to be fast or effective since it will only be used for boot-strapping. It might even be possible to make it as a pre-processor converting c++ into C.

          • Re:Seems odd... (Score:4, Interesting)

            by Philip_the_physicist ( 1536015 ) on Tuesday June 01, 2010 @06:10AM (#32416544)

            If they used an early enough variant of C++, they would be relatively straightforward, since that has already been done once (cfront), but templates would probably make things somewhat harder, and would probably require a decent macro language. If they want some of teh nicer-sounding features of C++0a, things might start getting very horrible quite quickly.

        • Maybe they've admitted that 'pride' is holding them back and that being able to use STL (for example) is a greater good than being able to do an initial compile on some obscure microcontroller which has a barely functioning C compiler.

          • by arth1 ( 260657 ) on Tuesday June 01, 2010 @06:48AM (#32416762) Homepage Journal

            Why, exactly, is using STL a greater good from a compiler side?
            For a C++ user, sure, but the compiler gains nothing from this, AFAICT.
            It's not like using STL makes code faster or less memory hungry, which, second to the algorithms, should be the focus.

            If I were to guess, allowing C++ serves one real purpose: bringing in the large amount of programmers out there that work with C++. Not because their running code is superior, but because they are there.

            • by ommerson ( 1485487 ) on Tuesday June 01, 2010 @07:34AM (#32417054)
              Quite simply because STL is the embodiment of several decades of algorithms and data structures research work. In many cases, use of STL results in near optimal code. In raw C, you're left to yourself to write your own collections and algorithms. You have to try pretty hard to surpass the performance of STL. Do you want compiler developers to constantly reinventing wheels or actually improving the compiler?
              • Re: (Score:3, Insightful)

                by koro666 ( 947362 )

                But then, crappy programmers misuse them, not knowing about what is done behind their back, and it becomes slow and bloated code.

                Having to specify everything explicitely makes you aware of the complexity/memory usage of what you are doing.

                • by pdusen ( 1146399 ) on Tuesday June 01, 2010 @10:52AM (#32419324) Journal
                  Crappy programmers are crappy whether they lean on the STL or not. Their implementation of pre-existing STL containers and algorithms is bound to be terrible.
                • by ultranova ( 717540 ) on Tuesday June 01, 2010 @11:51AM (#32420226)

                  But then, crappy programmers misuse them, not knowing about what is done behind their back, and it becomes slow and bloated code.

                  This is a compiler we are talking about. I think that we can assume that people who program the program that turns code into machine code must "know their shit", so to say - otherwise the time taken to compile will be the least of the user's problems.

                  Besides, having people copy code from a webpage/programming manual doesn't improve things any.

                  Having to specify everything explicitely makes you aware of the complexity/memory usage of what you are doing.

                  No, they simply memorize magical mantras that, when regurgitated, will do what they want. It's much better to give such people as high-level libraries as possible and let them use those; the more they have to think about optimization, the more likely they are to do something unbelievably stupid [thedailywtf.com].

                  Besides, the exact same argument could be used to condemn first OO, then structural programming, then anything that gets compiled, then finally machine code itself as an abstraction over the physical hardware of modern processors.

              • Re: (Score:3, Interesting)

                by tomhudson ( 43916 )

                Quite simply because STL is the embodiment of several decades of algorithms and data structures research work. In many cases, use of STL results in near optimal code. In raw C, you're left to yourself to write your own collections and algorithms. You have to try pretty hard to surpass the performance of STL. Do you want compiler developers to constantly reinventing wheels or actually improving the compiler?

                Since this is Troll Tuesday I'll be blunt - that's absolute horsheshit!

                In testing, performance can

                • by XDirtypunkX ( 1290358 ) on Tuesday June 01, 2010 @10:23AM (#32418884)

                  Oddly enough, STL contains a bsearch algorithm that works on variable length arrays and generates code which is pretty damn optimal. It also contains a highly optimized quicksort implementation (along with other sorting and inserting algorithms) that you can use to keep your array sorted. However, even the standard vector operations compile down to pretty much raw pointers if you use iterators, so you can use quicksort/bsearch with no extra penalty on a vector and all the work is done for you.

                  So it sounds awfully what you're saying is absolute horse-shit.

                  • Re: (Score:3, Interesting)

                    by sim82 ( 836928 )
                    how true
                    I had this funny 'aha' moment doing some simple operations on all elements of an int array: using a stl::vector and an iterator is actually faster than a plain-c int-array indexed by the loop-counter. The stl version boils down to pure pointer arithmetics, while the c version has the indexing overhead. Sure you can do the equivalent in c (as fast as an stl iterator, surprise, surprise), but honestly I like to keep away from this kind of stuff, most of the time.
                • by HuguesT ( 84078 )

                  The STL definitely can make your code run faster. For instance, the standard way to sort an array in C is with quicksort(), which requires a function pointer argument. This function is called for each comparison.

                  In C++, with the STL, you would use the sort() algorithm, which is inlined by the compiler. No function call. The resulting code can be an order of magnitude faster !

                  Read this report [stanford.edu] for some code and illustrations. The C++ code is easy to write and in their example still faster than a hand-designed

            • by Eponymous Coward ( 6097 ) on Tuesday June 01, 2010 @07:52AM (#32417186)

              It's not like using STL makes code faster or less memory hungry

              Sometimes it does. For example, compare the stl sort routine with qsort. The stl version is declared with a predicate method that can be made inline. The C version is passed a pointer to a predicate function that can't be inlined. So, the C++ version can eliminate a function call with each compare.

              But this is library and compiler dependent. In theory you really need to know how your compiler and library perform. In practice, it's so mature that everybody is pretty fast these days.

            • More eyeballs (Score:4, Interesting)

              by tepples ( 727027 ) <.tepples. .at. .gmail.com.> on Tuesday June 01, 2010 @08:25AM (#32417448) Homepage Journal

              Why, exactly, is using STL a greater good from a compiler side?

              For example, a std::vector may become correct more quickly than a home-grown array list because more eyeballs will be looking at it. In addition, its operator [] can be overloaded to provide bounds checking.

          • Re: (Score:3, Insightful)

            by Anonymous Coward

            Pride?

            Come on now, let's be fair. They said that boot strapping with only C was the reasoning. If you've ever bootstrapped a compiler or even worked on cross compiling tool chains for a new platform, what they did is huge. There is a reason that GCC is the compiler just about everywhere. you don't have to like it and I'll agree that some of the reuse stuff C++ can provide is potentially huge but it's not pride, it's the desire to be useful.

  • Great (Score:3, Funny)

    by jimmydevice ( 699057 ) on Tuesday June 01, 2010 @04:05AM (#32415960)
    We have to pick through the preprocessor output to find the broken bits.
  • by trialcode ( 1400591 ) on Tuesday June 01, 2010 @04:19AM (#32416052) Homepage
    Great idea! This will surely help steal back users from LLVM/clang. The reason people are jumping ship is because they want a compiler written in C++, it has nothing to do with performance, licenses and/or features. Just thinking about those crunchy templates, page up and page down, makes my mouth water. I can't even begin to comprehend how they ever got anything done without templates.
    • Great idea!

      Indeed.

      This will surely help steal back users from LLVM/clang.

      I hope so.

      So, you don't think that writing in a higher level language will lead to better performance and features? Perhaps you should start campaining to have LLVM rewritten in assembly language (or the LLVM intermediate language).

      As for licenses, time will tell. Back in the Bad Old Days, every vendor and his dog had their own compiler. Because most vendors were not terribly good at writing compilers, writing portable code was really

  • Choices, choices (Score:4, Insightful)

    by Cee ( 22717 ) on Tuesday June 01, 2010 @04:22AM (#32416068)

    To paraphrase Einstein [wikiquote.org]:

    Make things as simple as possible, but not simpler.

    IMHO, one should use as high level language as possible, but not higher. One should never choose a lower level language than necessary only because it is hard core, the choice has to be based on something more substantial.

    I've met several C programmers having the knee-jerk reaction when they hear the word C++ that it's bloated and slow and hard. And tell me what, they haven't read Stroustrup's FAQ [att.com] lately. C++ can be very lean and mean indeed. As can C# (which I'm mostly using right now).

    • by daid303 ( 843777 ) on Tuesday June 01, 2010 @04:56AM (#32416236)

      If you want to link to any FAQ, don't forget the FQA about C++ http://yosefk.com/c++fqa/ [yosefk.com]

      Reading it will give you an inside on the many issues you can have with C++. I don't oppose C++, but You Have To Know What You Are Doing (TM). Or else all hell breaks lose. Fixing bad C is doable, fixing bad C++ is the 7th circle of hell.

      • by serviscope_minor ( 664417 ) on Tuesday June 01, 2010 @05:32AM (#32416384) Journal

        The C++ FQA is mostly a bunch of rhetoric and sophistry with a good scattering of half-truths thrown in for good measure. It is a classic propaganda piece as the falsehoods are spread very continuously spread and mixed with truthful pieces. That makes it hard to debunk in a short post as one has to go in to nit-picking detail in order to expose it for the hokum that it really is. Fortunately, you can use your favourite search engine to search tha annals of comp.langg.c++.moderated for such information.

        • Re: (Score:3, Insightful)

          They have a few good points though. On the iostream vs printf issue, they're right that printing formatted output with iostream is stupidly verbose.
          The following are indeed equivalent (actually, the stream version seems to be missing either "0x" or std::showbase)
          printf("0x%08x\n", x);
          std::cout std::hex std::setfill('0') std::setw(8) x std::dec std::endl;

          I'd much prefer it if it was:
          using namespace std;
          cout fixwidth(hex(x), 8, '0') endl;
          which is nearly as short as the printf line.

        • Re:Choices, choices (Score:5, Interesting)

          by murdocj ( 543661 ) on Tuesday June 01, 2010 @07:14AM (#32416908)
          I spent a lot of years developing in C, some time in C++ (w/o using the standard template library) and the last year and a half using C++ with stl.  So I think I have a pretty valid basis for comparison, and I'd say that C++ has far more ways to go wrong than C does.  With C, you pretty much know what you've got.  C++ has a number of subtle, nasty bear traps that can bite you.  It's true that if you know what you are doing, you can produce good code & get the job done, but that's true of any language, including assembler.

          Here's a couple of items off the top of my head:

          Default assignment operator: All you need to do is add a pointer to your class and suddenly code that you don't see causes a bug.  Yes, IF you know about this you can work around it.  That's true of anything.

          Overloaded operators: I was leafing through the original Stroustrup C++ book and found this paragraph about the stream output operator '<<':
          "But why <<? ... The operators < and > were tried, but the meanings "less than" and "greater than" were so firmly implanted in people's minds that the new I/O statements were for all practical purposes unreadable."

          Well, yes, when people see an operator, they "think" they know what it's doing.  It's interesting to me that in this very first case of overloading, Stroustrup ran into this fundamental problem, and had to choose a somewhat obscure operator to get around it.

          References: references aren't what most people think of as references.  They are pointers with syntactic sugar.  Try getting a reference to an element of a vector, and then doing something that causes the vector storage to be reallocation.  Voila, you have a "reference" that refers to junk.

          All of these aren't impossible problems.  They are extra issues, inherent in the language, that you simply don't have in C.  I think that C++ has a lot of interesting ideas, it has a lot of power, but ultimately it also has a LOT of problems.
          • Re: (Score:3, Interesting)

            by b4dc0d3r ( 1268512 )

            Speaking of "syntactic sugar", this link below is a good one. Someone above linked to it. I'll just add a little bit to it here. A reference is a pointer, and anyone who tries to tell you otherwise is a fool. It is there to make things more readable - it is not a copy of an object, and the simplest of exercises in the worst book I've read on the language make that very clear. So I don't know why people think they are anything other than fancy pointers.

            And they aren't talking about using the headache in

  • Incorrect headline (Score:5, Informative)

    by Letharion ( 1052990 ) on Tuesday June 01, 2010 @04:27AM (#32416098)
    The headline says "Use C++ instead of C" which is incorrect. C++ is, as made obvious from the text, an option, not a requirement.
  • by Viol8 ( 599362 ) on Tuesday June 01, 2010 @04:31AM (#32416116) Homepage

    Are they seriously trying to suggest that the people who work on developing and maintaining a C++ compiler are novices in C++??

    Sorry , am I missing something here?

    • by dgriff ( 1263092 ) on Tuesday June 01, 2010 @05:47AM (#32416452)

      Are they seriously trying to suggest that the people who work on developing and maintaining a C++ compiler are novices in C++??

      Car analogy time! You can be an expert car mechanic without knowing how to drive.

      I'll get me coat...

  • Looking at the GNU Coding Standard [gnu.org] which is used for gcc, whatever 'best practices' and style guideline they come with will make a good fireplace material [kernel.org] ...

  • Finally! (Score:2, Interesting)

    For this kind of job, C++ really is better than C. One of C++'s goals was to standardise things people were doing in C anyway (eg OOP). The advantage of C++ is that the compiler writes all the boilerplate for you. Not only that, but it does it right every single time. Every line of code you don't write is one that does not have a bug.

    The main problem is that it is easy to write a basic C compiler and thereby bootstrap a system. With GCC's cross-compiler abilities, this is less of a problem than it might oth

    • What do you mean, "return of bad old days"? Have you actually viewed GCC code? It's still full of #defines.

      Conversely, I'll keep rooting for LLVM because it's not GPL licensed.
      • Bad old days: did you ever try to write portable C or C++ code in the (say) mid 90's on UNIX? Or more recently on embedded platforms.

        They all sucked. Now most of them use GCC, life has got a whole lot better.

        That is what I mean by the bad old days.

        And I'll keep rooting for GCC because it is under the GPL. It acieved what Stallman set out to do, make life one whole heckuva lot better for end users (i.e. me, the schmuck writing code) of the compiler in this case through the use of Free software. So yeah. I li

        • Re: (Score:3, Insightful)

          by Renegade88 ( 874837 )
          I would like to hear explained how a "schmuck writing code" would be worse off in comparison with a compiler that is BSD(like) licensed.

          If you mention the possibility of some terrible corporation forking and making a proprietary version of the compiler, I'll be disappointed. That situation has no impact on the "schmuck".

          By the way, have you ever looked into how difficult it is to actually contribute a bug fix back to GCC? It's such a hindrance (that's putting it mildly) that patches don't get contribu
    • Re: (Score:3, Insightful)

      by bzipitidoo ( 647217 )

      Wait, people were doing OOP in C? I didn't get the impression that all the compiler people wanted C++ for the OOP. Maybe all that some want are the little syntax shortcuts and cleanups and relaxations of rules, like '&' in function declarations for pass by reference, not having to typedef structs in order to refer to structs by their bare names, and ability to declare variables in the middle of code blocks, things like that. Maybe they want member functions and polymorphism only to have cleaner synta

  • by o'reor ( 581921 ) on Tuesday June 01, 2010 @04:57AM (#32416242) Journal
    starting in 3... 2... 1...

    Here's somme ammo from bash.org [69.61.106.93]:

    Phil: C++ is java's uncle that never comes to visit, and had half his face blown off when he stepped on a landmine, also he's a pedophile.
    Phil: But he's the industry standard.
    David: and runs much faster
    Phil: He has to be able to run fast, he's a pedophile.

  • Maybe while they're at it, they can add in actually-useful error messages. See http://blog.llvm.org/2010/04/amazing-feats-of-clang-error-recovery.html [llvm.org] for some examples. It's shocking how user-hostile GCC is in comparison.

  • Safe subset (Score:5, Insightful)

    by steveha ( 103154 ) on Tuesday June 01, 2010 @05:13AM (#32416302) Homepage

    The GCC guys are not going crazy here. They are discussing what subset of C++ to allow.

    If you use all the wild features of C++, the results could be scary. For example, operator overloading is great if used judiciously, but if used badly it can make the code a mess. And if it is used at all, then it means that you can't look at one page from a printout and know for sure what that code does; you need to look at all the class functions to make sure there aren't tricky overloaded operators.

    I use plain C all the time at work, and the top C++ feature they should be using is simply the object-oriented class stuff. With a single global namespace you need to make functions like MyClassAddFloatAndInt(), but in C++ you could just call that function add(); it would be part of MyClass, and if you have other "add" functions with other type signatures, they won't collide. They could go from:

    {
            MyClass m;
            MyClassInitialize(&m, foo, bar);
            MyClassAddFloatAndInt(&m, 3.0f, 2);
            MyClassDoSomething(&m);
            MyClassCleanup(&m);
    }

    to:

    {
            MyClass m(foo, bar);
            m.add(3.0f, 2);
            m.do_something();
    }

    Even better if they allow the use of C++ namespaces to keep a large project organized.

    The other major win that comes to mind is simply being able to use powerful C++ libraries like the STL. Not having to cook up some kind of container data structure in plain C, but being able to use std::vector<SomeType> and std::map<SomeType, OtherType> and such is a huge win.

    P.S. I read through much of the discussion and here was my favorite post:

    http://gcc.gnu.org/ml/gcc/2010-05/msg00757.html [gnu.org]

    steveha

    • For example, operator overloading is great if used judiciously, but if used badly it can make the code a mess. And if it is used at all, then it means that you can't look at one page from a printout and know for sure what that code does; you need to look at all the class functions to make sure there aren't tricky overloaded operators.

      There is nothing special about operator overloading. In any good language, you get used to thinking about operators as functions with a funny syntax. That removes all the myste

      • by Skapare ( 16644 )

        Any language can be abused. GCC does that a lot with C already and it is widely known (GLIBC is worse, BTW). The issue might well just be that with this fact known, they want to avoid having that problem just get worse with C++.

    • Yes...one of the BIG wins in OO programming is that each class works like a mini namespace. It seems like a minor detail to the C++ bashers but in practice it removes a huge burden from the programmer - all function names can be short and logical.

    • by r00t ( 33219 ) on Tuesday June 01, 2010 @06:06AM (#32416530) Journal

      grep MyClassAddFloatAndInt *.c

      grep add *.c

      This totally sucks. Now I need some complicated language-specific search tool that is sure to have fewer options than grep. It's probably not even scriptable, and surely much slower. Why do you want me to suffer?

  • by bluefoxlucid ( 723572 ) on Tuesday June 01, 2010 @07:47AM (#32417146) Homepage Journal
    Awesome! Now the compilation process can spend 80% of its time repeatedly linking virtual tables and getting confused over pure virtuals, instead of spending 5% of its time running through the symbol table and 95% compiling. Remember, building i.e. X11 invokes gcc hundreds or thousands of times, once for each C file (and it invokes the C preprocessor too, and all kinds of other shit from the toolchain, once for each included file each time it's included).

Never test for an error condition you don't know how to handle. -- Steinbach

Working...