NO

Author Topic: Should I use #pragma once or the include guard macro?  (Read 6423 times)

Offline PaoloC13

  • Member
  • *
  • Posts: 44
Should I use #pragma once or the include guard macro?
« 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?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Should I use #pragma once or the include guard macro?
« Reply #1 on: May 31, 2016, 11:08:32 AM »
Look here.
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.
« Last Edit: May 31, 2016, 12:00:12 PM by TimoVJL »
May the source be with you

Offline PaoloC13

  • Member
  • *
  • Posts: 44
Re: Should I use #pragma once or the include guard macro?
« Reply #2 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?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Should I use #pragma once or the include guard macro?
« Reply #3 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
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

JohnF

  • Guest
Re: Should I use #pragma once or the include guard macro?
« Reply #4 on: June 02, 2016, 05:36:54 PM »
Code: [Select]
#pragma once
Does not seem to make any difference in speed.

John

« Last Edit: June 02, 2016, 05:41:43 PM by JohnF »

Offline PaoloC13

  • Member
  • *
  • Posts: 44
Re: Should I use #pragma once or the include guard macro?
« Reply #5 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?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Should I use #pragma once or the include guard macro?
« Reply #6 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.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Scripter

  • Guest
Re: Should I use #pragma once or the include guard macro?
« Reply #7 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.


Offline PaoloC13

  • Member
  • *
  • Posts: 44
Re: Should I use #pragma once or the include guard macro?
« Reply #8 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.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Should I use #pragma once or the include guard macro?
« Reply #9 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.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline PaoloC13

  • Member
  • *
  • Posts: 44
Re: Should I use #pragma once or the include guard macro?
« Reply #10 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.