# How should one glob all files that end with .c, .h, or .S in a folder?

**URL:** https://discourse.cmake.org/t/how-should-one-glob-all-files-that-end-with-c-h-or-s-in-a-folder/9099
**Category:** Usage
**Created:** [September 27, 2023, 12:38am UTC](https://discourse.cmake.org/t/how-should-one-glob-all-files-that-end-with-c-h-or-s-in-a-folder/9099 "2023-09-27T00:38:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![f-zl](https://discourse.cmake.org/user_avatar/discourse.cmake.org/f-zl/32/3824_2.png) [@f-zl](https://discourse.cmake.org/u/f-zl)
#### Post date: [September 27, 2023, 12:38am UTC](https://discourse.cmake.org/t/how-should-one-glob-all-files-that-end-with-c-h-or-s-in-a-folder/9099/1 "2023-09-27T00:38:51Z")

</div>

I want to create a list of all .c, .h, .S files in a folder.

`file(GLOB SRCS *.[chS])` fails to match `.S` files.  
`file(GLOB SRCS *.[chs])` does match all those files (on Windows) but it doesn’t look correct because `.S` files end with capital `S`.

How should I write the glob pattern? Thanks.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 27, 2023, 9:32pm UTC](https://discourse.cmake.org/t/how-should-one-glob-all-files-that-end-with-c-h-or-s-in-a-folder/9099/2 "2023-09-27T21:32:17Z")

</div>

My first response would be “Don’t do it!”. Globbing for source files is actively discouraged by the CMake docs, and I regularly see problems in client projects that are caused by file globbing. In most cases, it is trivial to generate the list of files once and put that in your `CMakeLists.txt` file, and it is also typically trivial to maintain that list.

If you really must use file globbing (and there are times where it is unavoidable), then to answer your question, you can only use basic wildcard patterns. ~~There is no `[]` support.~~ (EDIT: There is `[]` support, I’d just never actually seen that used!) But you can list multiple patterns, which allows you to get the result you’re after:

```cmake
file(GLOB SRCS *.c *.h *.S)

```

If you are going to use `file(GLOB...)`, I urge you to consider using the `CONFIGURE_DEPENDS` keyword. That won’t solve all problems, but it will at least reduce them.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 27, 2023, 9:36pm UTC](https://discourse.cmake.org/t/how-should-one-glob-all-files-that-end-with-c-h-or-s-in-a-folder/9099/3 "2023-09-27T21:36:18Z")

</div>

Leaving my previous comment there, I since realised your question is more about the `[...]` part of the globbing pattern. Please try my alternative with three separate patterns. If that also shows the same problem with it not matching `*.S` files on Windows, that may warrant further investigation.

Please also indicate what CMake version you’re using.
