# How to specify the name of the file to generate pre-compiled headers?

**URL:** <https://discourse.cmake.org/t/how-to-specify-the-name-of-the-file-to-generate-pre-compiled-headers/12887>\
**Category:** Code\
**Created:** [November 2, 2024, 10:30am UTC](https://discourse.cmake.org/t/how-to-specify-the-name-of-the-file-to-generate-pre-compiled-headers/12887 "2024-11-02T10:30:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![perdrix](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/a8b319/32.png) [@perdrix](https://discourse.cmake.org/u/perdrix)\
**Post date:** [November 2, 2024, 10:30am UTC](https://discourse.cmake.org/t/how-to-specify-the-name-of-the-file-to-generate-pre-compiled-headers/12887/1 "2024-11-02T10:30:45Z")

</div>

I have this in my CMakeList file:

```auto
target_precompile_headers(DeepSkyStacker PRIVATE
	stdafx.h
	)

```

I also have an existing file to generate the pre-compiled headers called stdafx.cpp

However CMake creates a file called cmake\_pch.cxx (and indeed cmake\_pch.hxx which includes the header I specified.

Can I get CMake to use the existing stdafx.cpp instead?

Thanks, David

---

<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:** [November 2, 2024, 9:22pm UTC](https://discourse.cmake.org/t/how-to-specify-the-name-of-the-file-to-generate-pre-compiled-headers/12887/2 "2024-11-02T21:22:24Z")

</div>

No, that’s not how CMake’s precompiled headers work. You tell it which headers you want included in the set of precompiled headers, and it takes care of generating appropriate files for the toolchain you’re using, then injecting the precompiled headers into your build. You will need to remove `stdafx.cpp` from your project if you want to use CMake’s precompiled headers feature.

Note that you don’t need `stdafx.h` either. In your sources and headers, you should include just the headers each file actually needs. Then use `target_precompile_headers()` to specify the headers you want to include in the precompiled set (this would be the same list of headers you’re currently including in `stdafx.h`).
