Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to fix header guards using guardonce

📅 2019-Jan-28 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

Header guards are used in C and C++ header files to avoid them being included more than once during the compilation of a compilation unit. There are generally two techniques: using an include guard and using a pragma once directive. guardonce provides a set of Python tools that can be used to diagnose the state of header guards, convert between the two types of guards and to fix guards in your codebase.

$ sudo pip3 install guardonce
$ guard2once *.h
$ once2guard *.h
$ once2guard -p "name | snake | upper | prepend FOOBAR_" *.h

For a filename linkedList.h the above command would generate an include guard name FOOBAR_LINKED_LIST_H.

$ once2guard -s "#endif // %" *.h
$ once2guard -l *.h
$ guard2once *.h ; once2guard *.h

Tried with: guardonce 2.4.0 and Ubuntu 18.04