Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does #pragma once add inclusion guards?

Tags:

c++

c

#pragma once is not standard, but is supported by compilers like gcc and VC++. It helps to avoid inclusion guards.

But, internally, does the compiler add inclusion guards for #pragma once? If not, how does the compiler ensure that such a header is included only once?

like image 922
Pranit Kothari Avatar asked Jul 19 '13 14:07

Pranit Kothari


1 Answers

I'm sure it works just like include_once in PHP - there is a table of "files that has been included". The compiler, in this case, looks in the list for the file it is about to include and if a file has already been included, don't include it again. If the compiler, while processign the file, sees a #pragma once, then add this file to "files that have already been included".

So it's not the same as inclusion guards on a detail level, but it has the same effect as inclusion guards. It also makes the code less portable, since there are plenty of compilers that doesn't support this.

like image 53
Mats Petersson Avatar answered Oct 13 '22 11:10

Mats Petersson