36 lines · plain
1.. title:: clang-tidy - portability-avoid-pragma-once2 3portability-avoid-pragma-once4=============================5 6Finds uses of ``#pragma once`` and suggests replacing them with standard7include guards (``#ifndef``/``#define``/``#endif``) for improved portability.8 9``#pragma once`` is a non-standard extension, despite being widely supported10by modern compilers. Relying on it can lead to portability issues in11some environments.12 13Some older or specialized C/C++ compilers, particularly in embedded systems,14may not fully support ``#pragma once``.15 16It can also fail in certain file system configurations, like network drives17or complex symbolic links, potentially leading to compilation issues.18 19Consider the following header file:20 21.. code:: c++22 23 // my_header.h24 #pragma once // warning: avoid 'pragma once' directive; use include guards instead25 26 27The warning suggests using include guards:28 29.. code:: c++30 31 // my_header.h32 #ifndef PATH_TO_MY_HEADER_H // Good: use include guards.33 #define PATH_TO_MY_HEADER_H34 35 #endif // PATH_TO_MY_HEADER_H36