include_guard.rst 1.8 KB

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