Skip to main content

Utilities for converting from C/C++ include guards to #pragma once and back again

Project description

Why Convert?

Include guards suck. They’re tiring to type and tedius to update. Worse, the task of updating boilerplate leaves room for copy/paste errors, or other mistakes. #pragma once is simpler and less error prone. That’s why you should convert to #pragma once.

Alas, though #pragma once is available on all the most commonly used compilers, it’s not available on every compiler. Perhaps one day you’ll add support for a platform with a barebones compiler with no support for #pragma once and you’ll have to convert back. That’s ok. It’s easy!

What exactly is guardonce?

There are three main tools provided by guardonce:

  1. checkguard helps find any broken include guards you may already have in your project. These should be addressed before converting.

  2. guard2once converts files with include guards into files with #pragma once directives. This ensures your entire project is consistently using #pragma once.

  3. once2guard converts files with #pragma once directives back into files with include guards. This ensures your entire project is consistently using include guards.

How to use:

First, check your project for broken headers. To recursively search your project directories for the names of all files that lack proper include guards, use the following command, substituting your project’s directory for the quoted string:

checkguard -r "source_directory"

By default, checkguard is very forgiving. It accepts either #pragma once or anything that looks like an include guard. If you know that all your guards should match some format, you can be more strict by using -p to specify a pattern to check against.

If certain files are not supposed to have include guards, feel free to leave them be. Files without include guards are ignored by this next step.

Now, all that remains is converting the headers to use #pragma once:

guard2once -r "source_directory"

You’re done! Double check that the result matches your expectations and start using #pragma once in your new code. Know that if you ever need to switch back, it’s as simple as:

once2guard -r "source_directory"

If the default guard style doesn’t appeal to you, there are a few options to customize it. Maybe take a look through once2guard --help or check out a walkthrough for some examples.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

guardonce-2.4.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

guardonce-2.4.0-py2.py3-none-any.whl (15.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file guardonce-2.4.0.tar.gz.

File metadata

  • Download URL: guardonce-2.4.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

File hashes

Hashes for guardonce-2.4.0.tar.gz
Algorithm Hash digest
SHA256 c7891e519a1dde5d2e47de2927f554b5d1cc4ebb6f0d3eafb95bc09a5c60276b
MD5 e260a1c1064c413003e5aa0a420066a3
BLAKE2b-256 cefaccd538dd490fae9a01c75ca750c3f17d4dcc8783202f97fb17342c005d1f

See more details on using hashes here.

File details

Details for the file guardonce-2.4.0-py2.py3-none-any.whl.

File metadata

  • Download URL: guardonce-2.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

File hashes

Hashes for guardonce-2.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 325b5705148ca82b0422439e94456dd3b1a561556e2cc0e24a1acc1b8916f89f
MD5 f0598263ea3cf0314c53248b566a5ca2
BLAKE2b-256 7d05fec26708b5292a8c579a3393c498d46eb9e0f78f2a9b45c3058d4911aa1f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page