Pelles C forum

C language => Beginner questions => Topic started by: PaoloC13 on May 31, 2016, 01:49:15 AM

Title: Should I use #pragma once or the include guard macro?
Post by: PaoloC13 on May 31, 2016, 01:49:15 AM
To preventing multiple inclusion of header files, in a project, what would you recommend to do?
Using the include guard macro, or the #pragma once directive?

Are they both the same, or is there any reason whereby we should preferably use the one rather than the other?
Title: Re: Should I use #pragma once or the include guard macro?
Post by: TimoVJL on May 31, 2016, 11:08:32 AM
Look here (http://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards).
Not exactly same.
#pragma once prevent opening file with same name, so no parsing after this.

For other compilers use both.

Simple test, uncomment lines to see what happens:
test_pragma_once.c
Code: [Select]
#include "test_pragma_once.h"
#include "test/test_pragma_once.h"
#include "test_pragma_once.h"
#include "test\test_pragma_once.h"
int main(void) {return 0;}
test_pragma_once.h
Code: [Select]
#pragma once
#pragma message("1. test_pragma_once.h")
#ifndef _TEST_PRAGMA_ONCE_H
#define _TEST_PRAGMA_ONCE_H
#pragma once  // this prevent include in same name in same folder
#pragma message("test_pragma_once.h")
#endif // _TEST_PRAGMA_ONCE_H
test\test_pragma_once.h
Code: [Select]
#pragma once
#pragma message("2. test/test_pragma_once.h")
#ifndef _TEST_PRAGMA_ONCE_H  // this prevent include in same name in different folder
#define _TEST_PRAGMA_ONCE_H
#pragma once
#pragma message("test/test_pragma_once.h")
#endif // _TEST_PRAGMA_ONCE_H
Code: [Select]
Building test_pragma_once.obj.
1. test_pragma_once.h
test_pragma_once.h
2. test/test_pragma_once.h
Done.
Title: Re: Should I use #pragma once or the include guard macro?
Post by: PaoloC13 on June 02, 2016, 02:47:25 AM
Thanks Timo,
Reading your post and your link, it seems that there is not a simple answer.
Now I am a bit confused. I might conclude that the best choice for all my PellesC projects will be using the #pragma once directive making sure that any project will never accept files with the same name in different folder.

Could it be a good choice?
Title: Re: Should I use #pragma once or the include guard macro?
Post by: frankie on June 02, 2016, 04:24:01 PM
The common practice is to use the standard #ifdef/#define and inside use the #pragma once checking if supported.
For PellesC could be like this:
Code: [Select]
#ifndef _MYHEADER_H
#define _MYHEADER_H

/* If using a compiler version that supports it use pragma once */
#if __POCC__ >= 500
#pragma once
#endif

//My defines here
....

#endif    //_MYHEADER_H
Title: Re: Should I use #pragma once or the include guard macro?
Post by: JohnF on June 02, 2016, 05:36:54 PM
Code: [Select]
#pragma once
Does not seem to make any difference in speed.

John

Title: Re: Should I use #pragma once or the include guard macro?
Post by: PaoloC13 on June 02, 2016, 05:53:51 PM
Code: [Select]
#ifndef _MYHEADER_H
#define _MYHEADER_H

/* If using a compiler version that supports it use pragma once */
#if __POCC__ >= 500
#pragma once
#endif

//My defines here
....

#endif    //_MYHEADER_H

Why the #pragma once inside the #ifndef and not outer?
Title: Re: Should I use #pragma once or the include guard macro?
Post by: frankie on June 02, 2016, 09:20:48 PM
Why the #pragma once inside the #ifndef and not outer?
I think, as often happen in coding, at introduction of pragma once this was requested to avoid to apply it more than once, then it become almost a standard.
Title: Re: Should I use #pragma once or the include guard macro?
Post by: Scripter on June 03, 2016, 09:04:23 PM
To preventing multiple inclusion of header files, in a project, what would you recommend to do?
Using the include guard macro, or the #pragma once directive?

Are they both the same, or is there any reason whereby we should preferably use the one rather than the other?

Best to use the ...

#ifndef HEADER_H
#define HEADER_H
// your stuff here
#endif

... format since only a relatively few compilers support the prgama.

Title: Re: Should I use #pragma once or the include guard macro?
Post by: PaoloC13 on June 06, 2016, 02:04:57 PM
Best to use the ...

#ifndef HEADER_H
#define HEADER_H
// your stuff here
#endif

... format since only a relatively few compilers support the prgama.

as evidenced by TimoVJL, the two options are not exactly the same thing.
Moreover, frankie has used:

#if __POCC__ >= 500
#pragma once
#endif

which reduces the code to the usual #ifndef macro scheme, if the compiler does not support the #pragma once directive.
Mostly, I wonder what is the most appropriate choice using specifically the PellesC compiler, which supports it.
Title: Re: Should I use #pragma once or the include guard macro?
Post by: frankie on June 06, 2016, 05:20:49 PM
If you compile only with a PellesC compiler, which version is >=5.00, simply use #pragma once.
Title: Re: Should I use #pragma once or the include guard macro?
Post by: PaoloC13 on June 06, 2016, 07:01:45 PM
If you compile only with a PellesC compiler, which version is >=5.00, simply use #pragma once.

Ok frankie, I think I'll do it this way, maybe being careful not to use the same file into other parts of a project.
The directive is also supported by almost all modern compilers.