Damir
Damir

Reputation: 56199

How to remove GCC warning on #pragma region?

How to remove GCC warning on #pragma region ? I added pragma region to easily look at code but it reports warnings on #pragma region. I am using Visual Studio 2010.

Upvotes: 15

Views: 21808

Answers (7)

turkeyonly
turkeyonly

Reputation: 1

This works for me.

#if 1  // #pragma region
#endif // #pragma endregion

Upvotes: 0

maxbachmann
maxbachmann

Reputation: 3265

#pragma region support in compilers has improved since this question was asked. The following table lists compilers supporting this feature:

compiler supported since
msvc unknown since when this is supported
icc unknown since when this is supported
clang >= version 6 on all targets (earlier versions already support it on windows)
gcc >= version 13 (bug 85487)

Upvotes: 6

ZarakshR
ZarakshR

Reputation: 1573

If you comment the #pragma region and #pragma endregion lines, your editor might still be able to parse it and use the folding feature without gcc emitting any warnings.

I have tested it on VSCode 1.70.2 and this workaround works.

enter image description here

Upvotes: 1

OliverH
OliverH

Reputation: 51

It seems to be a MSVC specific pragma, so you should use

#ifdef _MSC_VER
#pragma region
#endif

<code here>

#ifdef _MSC_VER
#pragma endregion
#endif

Upvotes: 5

Dan
Dan

Reputation: 2528

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
... Code using Unknown pragmas ...
#pragma GCC diagnostic pop

Upvotes: 5

nos
nos

Reputation: 229108

gcc has this warning flag:

-Wunknown-pragmas Warn when a #pragma directive is encountered which is not understood by GCC. If this command line option is used, warnings will even be issued for unknown pragmas in system header files. This is not the case if the warnings were only enabled by the -Wall command line option.

And as per usual you can negate it, meaning unknown pragmas will not be given a warning. That is, use -Wno-unknown-pragmas.

Note that -Wno-unknown-pragmas must come after any command line flags that turn on this warning, such as -Wall - this also disables warnings on all unknown pragmas, so use with care.

Upvotes: 18

Some programmer dude
Some programmer dude

Reputation: 409176

Don't use it on GCC? :)

The simplest solution I can think of at the moment is to use the preprocessor conditionals for it:

#ifndef __GNUC__
#pragma region
#endif

// Stuff...

#ifndef __GNUC__
#pragma endregion
#endif

Not very good looking or readable, but will make the code compile without warnings on GCC.

Upvotes: 12

Related Questions