Should you use #pragma once?

Pragma as found by google image searchThe traditional way of using an include guard is to put all of the header inside a #ifdef like this.

#ifndef hr_time
  #define hr_time
  #include <linux/time.h>
  #include 

// rest of code here
#endif

However the modern way is to put this at the top of the header file.

#pragma once

And this seems supported by most compilers I’ve tried. Certainly Visual Studio C/C++, gcc and clang all work.

In fact when you add a new file and choose header in Visual Studio, it put the #pragma once in automatically for you!

Given that those three C compilers are the main ones I use, I much prefer this pragma and use it.  But I would be interested in hearing of any C compilers that don’t  use it.

 

Leave a Reply

Your email address will not be published. Required fields are marked *