4

I was trying to activate the option -Wmismatched-tags on gcc (detect inconsistent class/struct declaration, which might happens when using forward declaration), but on Ubuntu I get

c++: error: unrecognized command line option '-Wmismatched-tags'

My gcc version:

# gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.

Looking here it seems like it was integrated in gcc 4.9.0 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61339

Am I doing something wrong, or is the option actually not available anymore?

I am aware of Can GCC produce struct/class name mismatches like VS?, however the answer is 10 years old and I was hoping things had improved in the meantime.

Background: I need this in a project where we also build with clang, and where these inconsistencies are treated as error.

Antonio
  • 19,451
  • 13
  • 99
  • 197

1 Answers1

4

Looking here it seems like it was integrated in gcc 4.9.0 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61339

Actually, it doesn't. It was reported in 2014 but as OP explained in #5th post:

I can't speak for MS, but the original warning I posted was produced by clang. It does seem to worry about mismatched tags.

In gcc -Wmismatched-tags was introduced only in December 2019 and became a part of GCC 10 release:

$ git describe e8f1ade269a39ea86a76a2440818e1512ed480ee
basepoints/gcc-10-5517-ge8f1ade269a

Luckily, these days we have docker and official GCC releases are officially distributed as docker images:

$ docker search gcc
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
gcc                                The GNU Compiler Collection is a compiling s…   587                 [OK]
(...)

so you can just do:

docker run --rm -it gcc

and either mount your source directory into the docker container or use TRAMP integration for docker containers or any other preferred tool to put your code into the container and use the newest gcc inside it:

root@53d309d5d619:/# g++ --version
g++ (GCC) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

root@53d309d5d619:/# g++  hello.cpp  -o hello -Wmismatched-tags
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • 1
    There is actually a gcc-10 package for Ubuntu, it does come with 10.2! Now I have to figure out how to make it the default compiler, this recent guide should help https://linuxconfig.org/how-to-switch-between-multiple-gcc-and-g-compiler-versions-on-ubuntu-20-04-lts-focal-fossa – Antonio Mar 17 '21 at 15:37
  • Docker has still a huge advantage here - it can be used to create a portable development environment. Not everyone uses Ubuntu (I'm Slackware user for example) and some people don't use Linux at all (actually, Windows and MacOS are probably much more popular on desktops, even among developers). – Arkadiusz Drabczyk Mar 17 '21 at 15:39