Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is #pragma once a safe include guard?

I've read that there is some compiler optimization when using #pragma once which can result in faster compilation. I recognize that is non-standard, and thus could pose a cross-platform compatibility issue.

Is this something that is supported by most modern compilers on non-windows platforms (gcc)?

I want to avoid platform compilation issues, but also want to avoid the extra work of fallback guards:

#pragma once #ifndef HEADER_H #define HEADER_H  ...  #endif // HEADER_H 

Should I be concerned? Should I expend any further mental energy on this?

like image 582
Ryan Emerle Avatar asked Apr 24 '09 20:04

Ryan Emerle


People also ask

What are the meanings of is?

Definition of is (Entry 1 of 4) present tense third-person singular of be. dialectal present tense first-person and third-person singular of be. dialectal present tense plural of be.

What type of word is is?

Is is what is known as a state of being verb. State of being verbs do not express any specific activity or action but instead describe existence. The most common state of being verb is to be, along with its conjugations (is, am, are, was, were, being, been). As we can see, is is a conjugation of the verb be.

Is in use a word?

Definition of in use : being used All of the computers are currently in use.

What does Cambridge mean?

Cambridge. / (ˈkeɪmbrɪdʒ) / noun. a city in E England, administrative centre of Cambridgeshire, on the River Cam: centred around the university, founded in the 12th century: electronics, biotechnology.


2 Answers

#pragma once does have one drawback (other than being non-standard) and that is if you have the same file in different locations (we have this because our build system copies files around) then the compiler will think these are different files.

like image 92
Motti Avatar answered Sep 18 '22 15:09

Motti


Using #pragma once should work on any modern compiler, but I don't see any reason not to use a standard #ifndef include guard. It works just fine. The one caveat is that GCC didn't support #pragma once before version 3.4.

I also found that, at least on GCC, it recognizes the standard #ifndef include guard and optimizes it, so it shouldn't be much slower than #pragma once.

like image 33
Zifre Avatar answered Sep 20 '22 15:09

Zifre