Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Usability improvements in GCC 8

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

Share:

    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

    • How to set up NVIDIA NIM on Red Hat OpenShift AI

    • Leveraging Ansible Event-Driven Automation for Automatic CPU Scaling in OpenShift Virtualization

    • Python packaging for RHEL 9 & 10 using pyproject RPM macros

    • Kafka Monthly Digest: April 2025

    • How to scale smarter with OpenShift Serverless and Knative

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    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

    Red Hat legal and privacy links

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

    Report a website issue

    OSZAR »