#pragma once?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi. I'd like to use "#pragma once" in my .h files instead of the old fashioned #ifndef __FOO.H include guards,

   

It seems to work, but I get the warning symbol and the complaint "#pragma once in main file" in the left margin of the editor, although there are no actually compiler warnings.

   

Anyone know if this is OK to use, and if so, how to get rid of the warning in the editor?

   

Thanks!

0 Likes
1 Solution
AnkitaS_51
Employee
Employee
100 likes received 50 likes received 25 likes received

Hello,

   

Since there is no compiler warning it will not effect the functioning of application.

View solution in original post

0 Likes
6 Replies
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Why do you want to get rid of the #ifdefs? Using #pragma once doesn't speed up the compiling.

   

Are you using the Keil compiler (for PsoC3) or gcc? This might actually be a bug in the syntax checker of Creator (I think its using LLVM under the hood for that).

0 Likes
Anonymous
Not applicable

Nope, I'm using the built-in PSoC 4 ARM compiler, not Keil.

   

I've used the #ifndef guards for years too, but I'm trying to modernize my use of C a little, and those guards are ugly. It's much nicer to just stick "#pragma once" on the first line of every .h file and not worry about accidentally closing the #ifndef, etc. Pure aesthetics.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Then you should raise a support case with Cypress.

0 Likes
AnkitaS_51
Employee
Employee
100 likes received 50 likes received 25 likes received

Hello,

   

Since there is no compiler warning it will not effect the functioning of application.

0 Likes
markgsaunders
Employee
Employee
50 sign-ins 10 solutions authored 5 solutions authored

We support C99 and #pragma once is not part of that definition. As a result we are reticent about enabling it. Our roadmap includes support for more third party software tools and so we're a bit wary of investing in non-standard extensions.

   

I agree that the #ifndef method is uglier, of course, and so I hope you are OK living with it.

Anonymous
Not applicable

OK, I guess I can do that. Although it's pretty well supported for a nonstandard #pragma, including by the ARM compiler, Keil (http://www.keil.com/support/man/docs/armclang_ref/armclang_ref_chr1359124989221.htm) and IAR. Although ARM doesn't recommend it, most books on modern C programming do.

   

Portability (from Wikipedia page on #pragma once:
Clang Supported[7]
Comeau C/C++ Supported[8]
C++Builder XE3 Supported[9]
Digital Mars C++ Supported[10]
GCC Supported[11] (since 3.4[5])
HP C/aC++ Supported[citation needed] (since at least A.06.12)
IBM XL C/C++ Supported[12] (since 13.1.1)
Intel C++ Compiler Supported[13]
Microsoft Visual C++ Supported[14] (since 4.2)
Pelles C Supported[15]
ARM DS-5 Supported[16]
IAR C/C++ Supported[17]
Oracle Developer Studio C/C++ Supported[18] (since 12.5)
Portland Group C/C++ Not supported[19]

0 Likes