# about include\_guard

**URL:** https://discourse.cmake.org/t/about-include-guard/15737
**Category:** Usage
**Created:** [July 5, 2026, 3:19pm UTC](https://discourse.cmake.org/t/about-include-guard/15737 "2026-07-05T15:19:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![yushang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yushang/32/4574_2.png) [@yushang](https://discourse.cmake.org/u/yushang)
#### Post date: [July 5, 2026, 3:19pm UTC](https://discourse.cmake.org/t/about-include-guard/15737/1 "2026-07-05T15:19:45Z")

</div>

Hi guys,

I have the following setup

`CMakeLists.txt`

```auto
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(sub)
add_subdirectory(sub2)

```

`tools.cmake`

```auto
include_guard(DIRECTORY) # A
message("tools.cmake")

```

`sub\CMakeLists.txt`

```auto
message(STATUS "in sub")
include(tools)
add_subdirectory(sub)

```

`sub\sub\CMakeLists.txt`

```auto
message(STATUS "in sub/sub")
include(tools)

```

`sub2\CMakeLists.txt`

```auto
message(STATUS "in sub2")
include(tools)

```

got the following output

```auto
-- in sub
tools.cmake
-- in sub/sub
-- in sub2
tools.cmake

```

change line `A` to `include_guard()` got the same output

```auto
-- in sub
tools.cmake
-- in sub/sub
-- in sub2
tools.cmake

```

does this mean `include_guard(DIRECTORY)` is the same as `include_guard()`?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [July 5, 2026, 4:12pm UTC](https://discourse.cmake.org/t/about-include-guard/15737/2 "2026-07-05T16:12:08Z")

</div>

The documentation clearly specifies:

DIRECTORY: The include guard applies within the current directory **and below**.

So the guard apply to the subdirectories…

---

<div class="post-metadata">

### Author: ![yushang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yushang/32/4574_2.png) [@yushang](https://discourse.cmake.org/u/yushang)
#### Post date: [July 6, 2026, 12:39am UTC](https://discourse.cmake.org/t/about-include-guard/15737/3 "2026-07-06T00:39:43Z")

</div>

`include_guard()` only apply to the current directory and not include its subdirectory? If so, the output should be

```auto
-- in sub
tools.cmake
-- in sub/sub
tools.cmake
-- in sub2
tools.cmake

```

but it is not.

---

<div class="post-metadata">

### Author: ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)
#### Post date: [July 6, 2026, 7:56am UTC](https://discourse.cmake.org/t/about-include-guard/15737/4 "2026-07-06T07:56:31Z")

</div>

> [@yushang](#):
>
> does this mean `include_guard(DIRECTORY)` is the same as `include_guard()`?

`include_guard()` has variable scope, `include_guard(DIRECTORY)` has directory scope, `include_guard(GLOBAL)` has global scope.

In your example, variable scope and directory scope have the same results.

---

<div class="post-metadata">

### Author: ![yushang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yushang/32/4574_2.png) [@yushang](https://discourse.cmake.org/u/yushang)
#### Post date: [July 6, 2026, 1:42pm UTC](https://discourse.cmake.org/t/about-include-guard/15737/5 "2026-07-06T13:42:17Z")

</div>

thank you. I got it. If put `include(tools)` in a `function`, `include_guard()` and `include_guard(DIRECTORY)` will behave differently.
