C++ wxWidgets Linux Win32 OS X Autoconf Bakefile Boost GCC GNU IMAP MSVC Perl Python SMTP STL TCP/IP Threads Unix ZSH



I've recently had to compile a C++ program extensively using #pragma once with Sun CC compiler that doesn't support this pragma, unlike Microsoft Visual C++ and g++ that we normally use and that do support it. I hoped to find an existing solution to replace the pragmas with the usual, and standard compliant, preprocessor guards but, to my surprise, couldn't find anything so I had to spend half an hour to write the script below which might be useful if you ever find yourself in the same situation.

The script simply replaces all occurrences of #pragma once in all the files passed to it with the header guards of the form FILENAME_H_, here is an example of its use:

$ unpragma-once Exception.h
$ git diff
diff --git a/Exception.h b/Exception.h
index 170b932..f55a95d 100644
--- a/Exception.h
+++ b/Exception.h
@@ -13,7 +13,8 @@
     @file Exception.h

-#pragma once
+#ifndef EXCEPTION_H_
+#define EXCEPTION_H_

 #include <wx/string.h>
 #include <wx/debug.h>       // for __WXFUNCTION__
@@ -256,3 +257,4 @@
         throw EXCEPTION(_("Internal program error.")); \
     } while ( 0 )

+#endif // EXCEPTION_H_
Currently there is no possibility to customize the guards but you can easily edit the script to use some other format, e.g. prepend a common prefix to all of them.

Please also notice that the script is very naive and doesn't detect occurrences of the pragma inside C comments, for example. Hopefully this shouldn't happen too often in practice (and wouldn't be very difficult to correct if it did) and the script will give an error if it detects the #pragma once twice which should give you a chance to notice the problem.


The script is available at unpragma-once and there is no installation required but you must have a working Perl, see the requirements section below.


You need a working installation of Perl (no specific minor version, even 5.6 should work, although I didn't test with anything so ancient) and ideally CPAN to get autodie from it. However if you're feeling adventurous, you can try removing use autodie; line from the script and running it even if you don't have and can't install this module as normally no errors should occur -- and if they do, you can always just revert to the latest version from your version control system anyhow.

Usage Instructions

Just run the script with the names of all the files you want to remove the pragma from, e.g. unpragma-once *.h.


This script is Free Software released under BSD license.

September 1, 2020
New gcc-warnings-tools script for C/C++ programmers for showing information about all the available warning options.
June 23, 2015
Release of where-included: a new tool for C/C++ programmers for finding the header file dependencies.
July 28, 2014
A new release of Bakefile, a makefile generator tool, is now available.
July 20, 2014
We released a Munin plugin allowing to monitor the parameters of Freebox (French ISP modem).
November 11, 2013
wxWidgets 3.0, the first stable release of wxWidgets since several years, is now available.
September 1, 2013
Yahoo! Groups redesign has unfortunately made inoperable our popular yahoo2mbox script.
August 22, 2013
Added apache-splice-logs tool page.
March 31, 2013
Added new svn-to-git migration article.
August 6, 2012
Minor mladmin update: fix the script to work with recent Perl versions.
July 25, 2012
New diff-pdf tool description added.
April 27, 2012
wxWidgets training course proposed by TT-Solutions has been updated to cover version 3.0, please see training page for more information including the plan and some examples.
December 5, 2011
Another new script to help dealing with removing #pragma once from your code if needed.