1
0

include_guard.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. include_guard
  2. -------------
  3. .. versionadded:: 3.10
  4. Provides an include guard for the file currently being processed by CMake.
  5. .. code-block:: cmake
  6. include_guard([DIRECTORY|GLOBAL])
  7. Sets up an include guard for the current CMake file (see the
  8. :variable:`CMAKE_CURRENT_LIST_FILE` variable documentation).
  9. CMake will end its processing of the current file at the location of the
  10. :command:`include_guard` command if the current file has already been
  11. processed for the applicable scope (see below). This provides functionality
  12. similar to the include guards commonly used in source headers or to the
  13. ``#pragma once`` directive. If the current file has been processed previously
  14. for the applicable scope, the effect is as though :command:`return` had been
  15. called. Do not call this command from inside a function being defined within
  16. the current file.
  17. An optional argument specifying the scope of the guard may be provided.
  18. Possible values for the option are:
  19. ``DIRECTORY``
  20. The include guard applies within the current directory and below. The file
  21. will only be included once within this directory scope, but may be included
  22. again by other files outside of this directory (i.e. a parent directory or
  23. another directory not pulled in by :command:`add_subdirectory` or
  24. :command:`include` from the current file or its children).
  25. ``GLOBAL``
  26. The include guard applies globally to the whole build. The current file
  27. will only be included once regardless of the scope.
  28. If no arguments given, ``include_guard`` has the same scope as a variable,
  29. meaning that the include guard effect is isolated by the most recent
  30. function scope or current directory if no inner function scopes exist.
  31. In this case the command behavior is the same as:
  32. .. code-block:: cmake
  33. if(__CURRENT_FILE_VAR__)
  34. return()
  35. endif()
  36. set(__CURRENT_FILE_VAR__ TRUE)