back to article Linux kernel minimum compiler raised to GCC 5.1, allowing potential C11 use

Linux creator and maintainer Linus Torvalds has merged a late change to the forthcoming 5.15 kernel code that raises the minimum compiler from GCC 4.9 to 5.1 – which may in future enable use of an updated version of the C programming language, C11. Previously, the minimum version of GCC (GNU Compiler Collection) was 4.9, for …

  1. karlkarl Silver badge

    "Now, one feature that _may_ be worth it is the loop counter thing: for (int i = 10; i; i--) kind of syntax actually makes sense and is a real feature (it makes 'i' local to the loop, and can actually help people avoid bugs– you can't use 'i' by mistake after the loop)."

    Agreed, the all variables at start of a block really isn't a problem. Especially since for those who do like variables located near to where they are used, the start of a block can also be within if and while statements. Not just at the start of the function.

    As for for loops, this is possibly the only slightly annoying thing about C89. However for those projects where we do want to support simplistic / experimental compilers, the following is possible:

    {size_t i = 0; for(; i < len; ++i) {

    /* Use i */

    }}

    It possibly looks a little nasty but compared to the alternatives, I don't actually mind it. Though in this day and age C99 is generally the most common. I find C11 to be a little excessive. This shouldn't be like C++ where everyone chases version numbers like little fat kids wanting more, MORE, *MORE!* ;)

    1. Dan 55 Silver badge

      If you can only put variables at the start of a block and then you delete some code in a function, you're in danger of leaving some variables at the top which could be deleted.

      I suppose they'll be optimised away in the object code, but the source code should be clean and understandable.

      1. karlkarl Silver badge

        Very true.

        The -Wall and -pedantic flags *should* pick up on unused variables. Though sometimes we don't always get to use such capable compilers ;)

    2. Dave 15

      {size_t i = 0; for(; i < len; ++i) {

      what a ghastly way of writing it! Horrible. I want to know the starting condition of the loop. otherwise what you are really doing is a while, so why not write that?

      1. karlkarl Silver badge

        Re: {size_t i = 0; for(; i < len; ++i) {

        Haha. No it certainly isn't pretty. I do accept that.

        But you don't like it at all or do you propose something instead like:

        {size_t i; for(i = 0; i < len; ++i) {

        The issue with this is that my (admittedly very anal) static analyser complains on unassigned variables (and I actually like it that way). The starting condition is *kinda* including the variable declaration. Including things like:

        {size_t i = 0; size_t j = len; for(; i < len; ++i, --j) {

        I can't think of a way how a while loop could be used which is quite as succinct and encapsulated to the loop meaning.

        1. Anonymous Coward
          Anonymous Coward

          Re: {size_t i = 0; for(; i < len; ++i) {

          "(it makes 'i' local to the loop, and can actually help people avoid bugs– you can't use 'i' by mistake after the loop)."

          Available in other languages since the 1960s....

          1. Anonymous Coward
            Anonymous Coward

            Re: {size_t i = 0; for(; i < len; ++i) {

            Available in other languages since the 1960s....

            This.

            There is absolutely no reason for continued use of C when a small subset of C++ can achieve the same results with far better type safety. Using a bit more of the C++ language and its standard library and you remove the need for every C programmer to reinvent the wheel every time they need something more complicated than 'char' or 'int'. There is absolutely nothing that C can do that can't be done more easily and more safely in C++.

            1. karlkarl Silver badge

              Re: {size_t i = 0; for(; i < len; ++i) {

              Quite a strong view and I did used to think similar before I moved from C++ to C.

              C is consistently at the top of the TIOBE index: https://www.tiobe.com/tiobe-index/

              There must be a reason for that and what I arrived at was that C *is* the computing platform. It underlies everything, from interpreters, to graphics APIs to platform APIs. You can't get away from it, you can only layer on top.

              The issue with layering on top is you need bindings. Writing these blasted things is extremely time consuming (and unsafe!). You then run into interop issues. Getting Python to call a Java library is almost impossible without going through C. Just writing everything in C (with good linters, sanitizers) is liberating from all this complexity.

              I am almost convinced that C++ is only alive because it is one of the very few languages that has direct native support for C (with minor exceptions). My guess is that C++ will outlive Rust and Objective-C will ultimately outlast Swift because of this.

              1. Anonymous Coward
                Anonymous Coward

                Re: {size_t i = 0; for(; i < len; ++i) {

                > There must be a reason for that and what I arrived at was that C *is* the computing platform. It underlies everything, from interpreters, to graphics APIs to platform APIs. You can't get away from it, you can only layer on top.

                C might be the 'computing platform' but we should still do what we can to move away from it. Have a read of C is not a low-level language

                1. ChrisC Silver badge

                  Re: {size_t i = 0; for(; i < len; ++i) {

                  It might not be a low level language for the types of system the author of that article is considering. There are however other types of system still in existence, where the concerns raised in the article don't exist, and where C therefore remains a sensible choice.

              2. Anonymous Coward
                Anonymous Coward

                Re: {size_t i = 0; for(; i < len; ++i) {

                Yeah, the one limiting problem with C++ is the lack of a standardised ABI. I'll give you that.

                However, I don't believe that's a good enough reason to not use C++ internally. I write almost exclusively in C++ these days across multiple platforms (and have done for over 20 years, mostly for Windows, Linux, MacOS, iOS, and Android these days, but Xbox, Playstation, and other games consoles in the past) and you can get away with C++ 99.99% of the time.

                When you need interoperability with other languages a very thin C wrapper is usually the easiest way to go, unfortunately. But to be honest, that's not really a C wrapper per se, it's more about a standardised calling convention that happens to be the same as C's.

                In my career I started out with C and used it almost exclusively for about seven years. Once I switched to C++ (around 1999), I have never seen the need, nor wanted, to go back.

              3. Anonymous Coward
                Anonymous Coward

                Re: {size_t i = 0; for(; i < len; ++i) {

                I started programming in C back in 1983, buying a copy of K & R to teach myself the language.

                Having learned Z80 machine code (no assembler) on the ZX80, C was a revelation. It's essentially high level assembler! I still love the language to this day.

                I liked Java, will use C# when given no choice and tinker with Python, Rust etc. For many modern languages, the problem lies in the incomprehensibly large number of libraries, not the language.

                You can do lots with the C standard libraries, and your brain won't start to boil and drain out of your ears while you use them! Yes, you have to write more code, although any seasoned C programmer has their own set of libraries to do common tasks in their application sphere. In the end, C is a solid, rational and consistent programming language. It's a bedrock that you can always build good software on. It does require that those who use it are careful and considered and capable of remembering the memory they just allocated and ensuring they return it when they are finished with it, even in exceptional cases.

                The constantly moving targets of C# and .Net, the system libraries and the evolution of the language with more obstacles to understanding is just part of what makes me dislike it. The billion frameworks, patterns and layers upon layers of assemblies creating objects like memory was infinite are what turns that dislike into hate. I believe it is already impossible for one person to understand all that Microsoft has spawned in the .Net and C# world. It has also created a world of cut and paste programmers who have no appreciation for the art of good programming, regardless of the language being used.

                I've avoided C++ like a plague carrier throughout my career once I'd seen how it encouraged the creation of utterly incomprehensible code with such ease. Such code is never properly documented (but it's obvious from the object oriented design!) and is an utter nightmare when it comes to maintenance as the original writers have long since moved on to greener pastures.

                It's good to hear of someone moving to C from C++ and enjoying that feeling of being in charge when you use it. Welcome to the club!

  2. Anonymous Coward
    Anonymous Coward

    "Agreed, the all variables at start of a block really isn't a problem. Especially since for those who do like variables located near to where they are used, the start of a block can also be within if and while statements. Not just at the start of the function."

    Actually, the fact it was not possible in C89 was the reason I switched to ADA, back in the 90s.

    I was tired of reading single letter var names in messy code.

    But sadly, my boss objected since I was the only one developing in ADA ;)

    1. karlkarl Silver badge

      Perhaps I just saw C as an improvement from Awk where local variables had to be declared, not just at the start of a block but actually passed in as "unused" arguments to a function!

      function RecordSetId(ctx, id, _i, _j, _k) { ... }

      Useful language but... this is not its finest hour :D

      (I like Ada. Its difficulty is probably similar to what you experienced. Playing well with others and in particular, making all the rest of their unsafe platform libraries bind against it)

  3. Anonymous Coward
    Anonymous Coward

    GTK3 to GTK4 is a MUCH MORE OBJECTIONABLE change.......

    It's taken me years to get my brain wrapped around using Glade, GTK3 and C......to the point that I can just about write stuff that works!!

    *

    Then I get GTK4. Just read the advice for converting GTK3 to GTK4. HUNDREDS of statements like "Do not do X".

    *

    ....and Glade CAN'T be used with GTK4.

    *

    Some folk out there in the open source world JUST DON'T GET IT!!!!!!!!

    *

    gcc 4.9 to gcc 5.X......what's wrong with gcc11.2?......works fine here!!!

    1. matjaggard

      Re: GTK3 to GTK4 is a MUCH MORE OBJECTIONABLE change.......

      Your two statements are completely opposed to each other. On one hand you're saying "why do you make me change stuff" on the other you're saying "why don't they make more people change stuff?"

      People in the open source world absolutely DO get it. They might have subtly different opinions to you on the importance of backwards compatibility vs speed of movement forward but they know that they are making those choices and that's why you only had problems when upgrading full whole version numbers.

    2. DrXym

      Re: GTK3 to GTK4 is a MUCH MORE OBJECTIONABLE change.......

      GTK & Glade have become so antiquated that any change to modernise them and make the library more portable should be seen as a positive development.

      That said, GTK is still hamstrung by the lack of a simple, human readable declarative markup akin to QML. The XML that Glade pumps out is almost unreadable.

  4. Paul Crawford Silver badge

    Any one knowe if you get a proper warning if there are two definitions in use?

    In the past i have seen cases when you have, say, a globally defined variable and then a locally defined one of the same name (or in {} blocks which takes precedence but should cause a re-definition error so you don't get mixed up between the two. For example:

    void foo(void) {

    {int i;

    // some code here uses 'i'

    for(int i = 1; i<10; i++) {

    // Will new 'i' cause error?

    }

    }

    1. Anonymous Coward
      Anonymous Coward

      Most C++ compilers warn about this these days. Not sure about C compilers. They should do if they're any good.

    2. coconuthead

      This is called "shadowing" and clang at least can warn about it.

      Coming from Pascal, with its nested functions, I have no problem with shadowing and have that warning turned off.

      1. GrumpenKraut

        Use -Wshadow with GCC, not sure whether -Wall and/or -Wextra imply it.

  5. bombastic bob Silver badge
    Linux

    compiler masturbation

    I remember a specific comment/rant from Linux (that I agree with)

    related to whatever the "new, shiny" compiler features kiddies want to use now that they CAN... (but 'can' does not necessarily imply 'should').

    1. Charlie Clark Silver badge

      Re: compiler masturbation

      Maybe, but this move isn't really about that. You have to consider the changes in processor architectures so x86_64 and ARM are better served by newer compilers. The alternative, which is becoming increasingly common, is switching completely to CLANG.

      1. GrumpenKraut

        Re: compiler masturbation

        Whether in a project the default is GCC or CLANG, at least occasionally use the other compiler!

        1. bombastic bob Silver badge
          Devil

          Re: compiler masturbation

          One observation: if you build for FreeBSD _ and _ Linux, your project REALLY needs to build with clang AND gcc

          (at least if you do not want ports to force a gcc dependency)

          But for Linux kernel code you can usually make assumptions about the "official" compiler that is to be used when building it.

          And as long as people write code that does not simply use a (new, shiny) feature for sake of using it (as with the aforementioned rant from Linus) newer compiler versions should be welcome. Ideally the code will still build with the older compilers, too. (but loop var declaration would prevent that, yeah).

          I think the standardization over gnu89 was because many older architectures (or maybe distros) did not have available compilers that were reliable enough to build the kernel. But if those older architectures simply do not get support from new compilers, they may get stuck at the previous kernel. (as mentioned in the article)

          and, of course, no "new, shiny" as the only motivation for using a compiler feature...

    2. DrXym

      Re: compiler masturbation

      GCC 5.1 has been out since 2015 so I wouldn't exactly describe it as new or shiny. And generally speaking the best strategy when moving up with a compiler is:

      a) stick to the standards and avoid compiler extensions or at least isolate the code that *needs* them.

      b) do not rewrite code for the sake of it, e.g. C99 code does not need to be made into C11 code without an incredibly good reason.

      As for why they might want to bump up the compiler, the most probable reason is for maintenance - to get rid of stupid workarounds, hacks and macros they needed for older compilers but can do away with to simplify the code base.

    3. karlkarl Silver badge

      Re: compiler masturbation

      Haha. Nice.

      To me this summarizes most C++ users who frequent reddit's r/cpp. They are too busy fantasizing about the very latest C++ standard rather than actually doing something productive.

      Weirdly you don't seem to get this kind of "version chasing" with C or even any other language. No-one gives a sh*t if it is Java 10 or Java 11. Yes there was breaking changes from Python 2 -> 3 but you don't get some guy complaining that Python 3.7 is ancient compared to Python 3.8 or singing praises about some "modern" feature of Lua.

      I wonder if it is because C++ has fairly clean defined standards or if it is something else. I think the worst offenders are C++, Javascript, Rust (possibly in that order).

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon

Other stories you might like