Home > Enterprise >  gcc - C error "declaration changes meaning" - error appears on linux gcc 11.3, but not o
gcc - C error "declaration changes meaning" - error appears on linux gcc 11.3, but not o

Time:08-12

With this code:

enum class profession
{
    doctor,
    banker
};

class person
{
    profession profession;
};

int main()
{
}

on linux (opensuse 15.4 with gcc 11.2), compilation command

g   -std=c  20 -pedantic -Wall -Wextra -Werror=return-type -Wshadow=local -Wempty-body -fdiagnostics-color -s -Os -o program_gpp program.cpp

I get the error : error: declaration of ‘profession person::profession’ changes meaning of ‘profession’ [-fpermissive]

while on windows (windows 10 pro, 21h1) / msys2 / mingw-w64 / gcc 12.1, compilation command:

g   -std=c  20 -pedantic -Wall -Wextra -Werror=return-type -Wshadow=local -Wempty-body -fdiagnostics-color -s -Os program.cpp -o program_gpp.exe

I don't get any warning or error, nothing.

I had already found somewhere that maybe gcc itself is built with different switches for the two platforms, and so its sensibility to some errors changes among them, and this is ok. But since I would like my code to avoid this error and compile everywhere, which gcc switches can I use to force this error also on the gcc on windows / msys2 ? note : I don't want to disable the error on linux (the compiler already tells me that it can be disabled with -fpermissive), I want gcc on windows to give me the error too so that I can fix it there and it will also compile on linux.

CodePudding user response:

Pass -fno-ms-extensions to MSYS2 GCC. This disables some MSVC-esque extensions.

  • Related