Skip to main content

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.

Metadata

Release files for guardonce 2.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for guardonce 2.4.0
File Size Uploaded
guardonce-2.4.0.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for guardonce 2.4.0
File Interpreter ABI Platform
guardonce-2.4.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 27.0 kB

Release files / guardonce-2.4.0.tar.gz

Download URL guardonce-2.4.0.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c7891e519a1dde5d2e47de2927f554b5d1cc4ebb6f0d3eafb95bc09a5c60276b
BLAKE2b-256 checksum
How to use checksums
cefaccd538dd490fae9a01c75ca750c3f17d4dcc8783202f97fb17342c005d1f
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / guardonce-2.4.0-py2.py3-none-any.whl

Download URL guardonce-2.4.0-py2.py3-none-any.whl
Size 15.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
325b5705148ca82b0422439e94456dd3b1a561556e2cc0e24a1acc1b8916f89f
BLAKE2b-256 checksum
How to use checksums
7d05fec26708b5292a8c579a3393c498d46eb9e0f78f2a9b45c3058d4911aa1f
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

2.4.0 This release

2 release files

2.3.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page