Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Usability improvements in GCC 8

March 15, 2018
David Malcolm
Related topics:
LinuxDeveloper toolsC, C#, C++
Related products:
Red Hat Enterprise Linux

    I work at Red Hat on GCC, the GNU Compiler Collection.

    My main focus for the last year has been on making GCC easier to use, so I thought I'd write about some of the C and C++ improvements I've made that are in the next major release of GCC, GCC 8. You can easily install GCC 8 on Red Hat Enterprise Linux 6 and 7 using Red Hat Developer Toolset (DTS).  GCC 8 is the default compiler in Red Hat Enterprise Linux 8 Beta. GCC 8 is also available in Fedora 28 and later.

    Helping you fix silly mistakes

    Quick quiz #1

    Quick - find the mistake in the following:

    int test(void)
    {
      return 42
    }
        
    

    In earlier releases of gcc, we printed the rather unhelpful:

    $ gcc t.c
    t.c: In function ‘test’:
    t.c:4:1: error: expected ‘;’ before ‘}’ token
     }
     ^
    

    For gcc 8, I've fixed things so that the location of the missing semicolon is properly highlighted:

    $ gcc t.c
    t.c: In function ‘test’:
    t.c:3:12: error: expected ‘;’ before ‘}’ token
       return 42
                ^
                ;
     }
     ~
    

    In particular, the error message is now showing the correct line. It also now suggests inserting the missing semicolon via a "fix-it hint", so that an IDE which supports them can offer to fix the issue for you (for example, Eclipse's CDT).

    Quick quiz #2

    Find the syntax error in:

    double MIN = 68.0;
    double MAX = 72.0;
    
    int logging_enabled;
    
    extern void write_to_log(double);
    extern int check_range(void);
    
    void log_when_out_of_range(double temperature)
    {
      if (logging_enabled && check_range ()
          && (temperature < MIN || temperature > MAX) {
        write_to_log (temperature);
      }
    }
    

    Older versions of gcc printed:

    $ gcc unclosed.c
    unclosed.c: In function ‘log_when_out_of_range’:
    unclosed.c:12:51: error: expected ‘)’ before ‘{’ token
           && (temperature < MIN || temperature > MAX) {
                                                       ^
    unclosed.c:15:1: error: expected expression before ‘}’ token
     }
     ^
    

    It's complaining about a missing close-parenthesis, but which open-parenthesis does it correspond to?

    gcc 8 now highlights the relevant opening parenthesis:

    $ gcc unclosed.c
    unclosed.c: In function ‘log_when_out_of_range’:
    unclosed.c:12:50: error: expected ‘)’ before ‘{’ token
           && (temperature < MIN || temperature > MAX) {
                                                      ^~
                                                      )
    unclosed.c:11:6: note: to match this ‘(’
       if (logging_enabled && check_range ()
          ^
    unclosed.c:15:1: error: expected expression before ‘}’ token
     }
     ^
    

    and provides a fix-it hint so that IDEs can offer to automate the fix. (Sadly, I wasn't able to fix that stray extra "expected expression before '}' token" error for gcc 8).

    If they're on the same line, it highlights it much more compactly:

    $ gcc unclosed-2.c
    unclosed-2.c: In function ‘test’:
    unclosed-2.c:8:45: error: expected ‘)’ before ‘{’ token
       if (temperature < MIN || temperature > MAX {
          ~                                      ^~
                                                 )
    unclosed-2.c:11:1: error: expected expression before ‘}’ token
     }
     ^
    

    I've also fixed how we handle:

    int i
    int j;
    

    for which earlier gcc releases used to print this inscrutable diagnostic:

    $ gcc q.c
    q.c:2:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
     int j;
     ^
    

    which was particularly bad if the two lines spanned two different header files.

    With gcc 8 it puts the error in the right place, and now prints:

    $ gcc q.c
    q.c:1:6: error: expected ‘;’ before ‘int’
     int i
          ^
          ;
     int j;
     ~~~
    

    which I find much easier to figure out.

    A trick question?

    What's wrong with the following code?

    const char *test(void)
    {
        FILE *f;
        int limit = INT_MAX;
    
        /* etc */
    
        return NULL;
    }
    

    This is a trick question - the code is fine, but, as is common with fragments of code seen on random websites, it's missing #include directives. If you simply copy this into a new file and try to compile it as-is, it fails.

    This can be frustrating when copying and pasting examples - off the top of your head, which header files are needed by the above? - so for gcc 8 I've added hints telling you which header files are missing (for the most common cases):

    $ gcc incomplete.c
    incomplete.c: In function ‘test’:
    incomplete.c:3:5: error: unknown type name ‘FILE’
         FILE *f;
         ^~~~
    incomplete.c:3:5: note: ‘FILE’ is defined in header ‘<stdio.h>’; did you forget to ‘#include <stdio.h>’?
    incomplete.c:1:1:
    +#include <stdio.h>
     const char *test(void)
    incomplete.c:3:5:
         FILE *f;
         ^~~~
    incomplete.c:4:17: error: ‘INT_MAX’ undeclared (first use in this function)
         int limit = INT_MAX;
                     ^~~~~~~
    incomplete.c:4:17: note: ‘INT_MAX’ is defined in header ‘<limits.h>’; did you forget to ‘#include <limits.h>’?
    incomplete.c:1:1:
    +#include <limits.h>
     const char *test(void)
    incomplete.c:4:17:
         int limit = INT_MAX;
                     ^~~~~~~
    incomplete.c:4:17: note: each undeclared identifier is reported only once for each function it appears in
    incomplete.c:8:12: error: ‘NULL’ undeclared (first use in this function)
         return NULL;
                ^~~~
    incomplete.c:8:12: note: ‘NULL’ is defined in header ‘<stddef.h>’; did you forget to ‘#include <stddef.h>’?
    incomplete.c:1:1:
    +#include <stddef.h>
     const char *test(void)
    incomplete.c:8:12:
         return NULL;
                ^~~~
    

    As before these fix-it hints can be made machine-readable (for use by an IDE) via -fdiagnostics-parseable-fixits, or you can use -fdiagnostics-generate-patch to have gcc emit:

    --- incomplete.c
    +++ incomplete.c
    @@ -1,3 +1,6 @@
    +#include <stdio.h>
    +#include <limits.h>
    +#include <stddef.h>
     const char *test (void)
     {
         FILE *f;
    

    Similarly, for C++, gcc now emits hints for missing "std" includes:

    std::string s("hello world");
    
    $ gcc incomplete.cc
    incomplete.cc:1:6: error: ‘string’ in namespace ‘std’ does not name a type
     std::string s("hello world");
          ^~~~~~
    incomplete.cc:1:1: note: ‘std::string’ is defined in header ‘<string>’; did you forget to ‘#include <string>’?
    +#include <string>
     std::string s("hello world");
     ^~~
    

    Parameter type mismatches

    I find a lot of my time is spent trying to call APIs, and dealing with silly mistakes like in the following:

    extern int callee(int one, const char *two, float three);
    
    int caller(int first, int second, float third)
    {
      return callee(first, second, third);
    }
    

    Older versions of GCC weren't very helpful when describing the problem:

    $ gcc arg-type-mismatch.cc
    arg-type-mismatch.cc: In function ‘int caller(int, int, float)’:
    arg-type-mismatch.cc:5:37: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
       return callee(first, second, third);
                                         ^
    arg-type-mismatch.cc:1:12: error:   initializing argument 2 of ‘int callee(int, const char*, float)’ [-fpermissive]
     extern int callee(int one, const char *two, float three);
                ^
    

    You were told the argument number, but then had to count commas in the source.

    For gcc 8, it prints this:

    $ gcc arg-type-mismatch.cc
    arg-type-mismatch.cc: In function ‘int caller(int, int, float)’:
    arg-type-mismatch.cc:5:24: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
       return callee(first, second, third);
                            ^~~~~~
    arg-type-mismatch.cc:1:40: note:   initializing argument 2 of ‘int callee(int, const char*, float)’
     extern int callee(int one, const char *two, float three);
                                ~~~~~~~~~~~~^~~
    

    which I hope you agree is much more readable: the compiler underlines both the problematic argument at the callsite and the corresponding parameter at the declaration of the callee, so you can immediately see the mismatch.

    Implementing this turned out to be much harder than you might expect. In particular, GCC's internal representation of expressions didn't make a distinction between the declaration of a parameter vs uses of that parameter, so there was no natural place to store the source location of the use of second in the example above. So I had to do a fair amount of work "under the hood" to make this happen.

    How do I get at some private field?

    I often find myself trying to get at a field of a C++ object, knowing the fieldname, but finding it's private, and then trying to remember the name of the accessor - was it get_foo, getFoo, read_foo, or just foo?

    So for GCC 8 I've added hints to the "field is private" error, showing how to use an accessor to get at the field in question, if one exists:

    For example, given:

    class foo
    {
    public:
      double get_ratio() const { return m_ratio; }
    
    private:
      double m_ratio;
    };
    
    void test(foo *ptr)
    {
      if (ptr->m_ratio >= 0.5)
        ;// etc
    }
    

    the compiler now gives this hint when complaining about the direct access:

    $ gcc accessor.cc
    accessor.cc: In function ‘void test(foo*)’:
    accessor.cc:12:12: error: ‘double foo::m_ratio’ is private within this context
       if (ptr->m_ratio >= 0.5)
                ^~~~~~~
    accessor.cc:7:10: note: declared private here
       double m_ratio;
              ^~~~~~~
    accessor.cc:12:12: note: field ‘double foo::m_ratio’ can be accessed via ‘double foo::get_ratio() const’
       if (ptr->m_ratio >= 0.5)
                ^~~~~~~
                get_ratio()
    

    Template type differences

    Compiler errors involving C++ templates have a reputation for being difficult to read. Here's what an older version of gcc emitted for a simple example:

    $ gcc templates.cc
    templates.cc: In function ‘void test()’:
    templates.cc:9:25: error: could not convert ‘vector<double>()’ from ‘vector<double>’ to ‘vector<int>’
       fn_1(vector<double> ());
                             ^
    templates.cc:10:26: error: could not convert ‘map<int, double>()’ from ‘map<int, double>’ to ‘map<int, int>’
       fn_2(map<int, double>());
                              ^
    

    In even this simple case, I find there's something of a "wall of text" problem where my eyes start to glaze over.

    For gcc 8 I've borrowed some good ideas from clang to improve such template diagnostics. I've added color to the messages, so that the mismatching parts of the template type are highlighted in green. Also, we now elide the parameters that are common between two mismatching templates, printing [...] instead:

    $ gcc templates.cc
    templates.cc: In function ‘void test()’:
    templates.cc:9:8: error: could not convert ‘vector<double>()’ from ‘vector<double>’ to ‘vector<int>’
       fn_1(vector<double> ());
            ^~~~~~~~~~~~~~~~~
    templates.cc:10:8: error: could not convert ‘map<int, double>()’ from ‘map<[...],double>’ to ‘map<[...],int>’
       fn_2(map<int, double>());
            ^~~~~~~~~~~~~~~~~~
    

    Those [...] elided parameters can be seen using -fno-elide-type:

    $ gcc templates.cc -fno-elide-type
    templates.cc: In function ‘void test()’:
    templates.cc:9:8: error: could not convert ‘vector<double>()’ from ‘vector<double>’ to ‘vector<int>’
       fn_1(vector<double> ());
            ^~~~~~~~~~~~~~~~~
    templates.cc:10:8: error: could not convert ‘map<int, double>()’ from ‘map<int,double>’ to ‘map<int,int>’
       fn_2(map<int, double>());
            ^~~~~~~~~~~~~~~~~~
    

    For more complicated errors, I implemented -fdiagnostics-show-template-tree, which visualizes the mismatching templates in a hierarchical form (for this rather contrived example):

    $ gcc templates-2.cc -fdiagnostics-show-template-tree
    templates-2.cc: In function ‘void test()’:
    templates-2.cc:9:8: error: could not convert ‘vector<double>()’ from ‘vector<double>’ to ‘vector<int>’
      vector<
        [double != int]>
       fn_1(vector<double> ());
            ^~~~~~~~~~~~~~~~~
    templates-2.cc:10:8: error: could not convert ‘map<map<int, vector<double> >, vector<double> >()’ from ‘map<map<[...],vector<double>>,vector<double>>’ to ‘map<map<[...],vector<float>>,vector<float>>’
      map<
        map<
          [...],
          vector<
            [double != float]>>,
        vector<
          [double != float]>>
       fn_2(map<map<int, vector<double>>, vector<double>> ());
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    which again works with -fno-elide-type:

    $ gcc templates-2.cc -fdiagnostics-show-template-tree -fno-elide-type
    templates-2.cc: In function ‘void test()’:
    templates-2.cc:9:8: error: could not convert ‘vector<double>()’ from ‘vector<double>’ to ‘vector<int>’
      vector<
        [double != int]>
       fn_1(vector<double> ());
            ^~~~~~~~~~~~~~~~~
    templates-2.cc:10:8: error: could not convert ‘map<map<int, vector<double> >, vector<double> >()’ from ‘map<map<int,vector<double>>,vector<double>>’ to ‘map<map<int,vector<float>>,vector<float>>’
      map<
        map<
          int,
          vector<
            [double != float]>>,
        vector<
          [double != float]>>
       fn_2(map<map<int, vector<double>>, vector<double>> ());
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    Plus a bunch of other stuff...

    The C++ frontend can now give you a hint if you use a macro before it was defined
    (e.g. if you mess up the order of your #includes):

    $ gcc ordering.cc
    ordering.cc:2:24: error: expected ‘;’ at end of member declaration
       virtual void clone() const OVERRIDE { }
                            ^~~~~
                                 ;
    ordering.cc:2:30: error: ‘OVERRIDE’ does not name a type
       virtual void clone() const OVERRIDE { }
                                  ^~~~~~~~
    ordering.cc:2:30: note: the macro ‘OVERRIDE’ had not yet been defined
    In file included from ordering.cc:5:
    c++11-compat.h:2: note: it was later defined here
     #define OVERRIDE override
    
    

    I added fix-it hints to the C++ frontend's -Wold-style-cast diagnostic, telling you whether you can use a static_cast, const_cast, reinterpret_cast etc:

    $ gcc -c old-style-cast-fixits.cc -Wold-style-cast
    old-style-cast-fixits.cc: In function ‘void test_1(void*)’:
    old-style-cast-fixits.cc:5:19: warning: use of old-style cast to ‘struct foo*’ [-Wold-style-cast]
       foo *f = (foo *)ptr;
                       ^~~
                ----------
                static_cast<foo *> (ptr)
    

    ...and so on.

    Trying it out

    See How to install GCC 8 on Red Hat Enterprise Linux 7. You can also try GCC 8 in RHEL 8 Beta. GCC 8 is also available in Fedora 28 and later.

    Alternatively, for simple code examples, you can play around with the new gcc at https://godbolt.org/.

    Have fun!

    You might also want to check out:

    • GNU Toolchain Update – Spring 2018 by Nick Clifton
    • Recommended compiler and linker flags for GCC by Florian Weimer
    • Detecting string truncation with GCC 8 by Martin Sebor
    Last updated: March 8, 2019

    Recent Posts

    • Protect data offloaded to GPU-accelerated environments with OpenShift sandboxed containers

    • Case study: Measuring energy efficiency on the x64 platform

    • How to prevent AI inference stack silent failures

    • Preventing GPU waste: A guide to JIT checkpointing with Kubeflow Trainer on OpenShift AI

    • How to manage TLS certificates used by OpenShift GitOps operator

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.