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.
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.