# CMake precompile header issue

**URL:** https://discourse.cmake.org/t/cmake-precompile-header-issue/8243
**Category:** Usage
**Tags:** comp:clang
**Created:** [June 2, 2023, 12:16am UTC](https://discourse.cmake.org/t/cmake-precompile-header-issue/8243 "2023-06-02T00:16:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![magisu](https://discourse.cmake.org/user_avatar/discourse.cmake.org/magisu/32/3260_2.png) [@magisu](https://discourse.cmake.org/u/magisu)
#### Post date: [June 2, 2023, 12:16am UTC](https://discourse.cmake.org/t/cmake-precompile-header-issue/8243/1 "2023-06-02T00:16:33Z")

</div>

I have the following header files

h1.h

```auto
#ifndef __HEADER1__
#define __HEADER1__

#include <iostream>
#include <string>

class Coupler {
private:
	const std::string file;
	const int line;
public:
	Coupler(const std::string& file, const int line): file(file), line(line) {
		std::cout << "Construct at " << file << ":" << line << std::endl;
	}

	~Coupler() {
		std::cout << "Destruct" << std::endl;
	}
};

#define coupler() Coupler( __FILE__ , __LINE__ )

#endif

```

h2.h

```auto
#ifndef __HEADER2__
#define __HEADER2__

#include <iostream>
#include <string>

inline void coupler() {
	std::cout << std::string("COUPLERCALLED") << std::endl;
}

#endif

```

main.cpp

```auto
#include "h2.h"

int exec() {
	coupler();
	return 100;
}

#include "h1.h"

int main() {
	coupler();
	std::cout << "main()" << std::endl;

	return 0;
}

```

And my CMakeLists.txt looks like

```auto
cmake_minimum_required(VERSION 3.20)

project(tttttt C CXX)

add_executable(tt ${CMAKE_SOURCE_DIR}/src/main.cpp)
target_include_directories(tt PRIVATE ${CMAKE_SOURCE_DIR}/include)

```

which builds the project fine. However, when I added precompiled header by introducing

```auto
target_precompile_headers(tt PRIVATE ${CMAKE_SOURCE_DIR}/include/h1.h ${CMAKE_SOURCE_DIR}/include/h1.h)

```

The compile failed as in h2.h, the `inline void coupler()` is macro-expanded and interpreted as a variable declaration, not a function. This seems caused by the issue that CMake is merging both h1.h and h2.h into a single precompiled header file. From the document I am not sure how to generate independent PCH for each single header file. Any ideas? Thanks a lot!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 7, 2023, 11:41am UTC](https://discourse.cmake.org/t/cmake-precompile-header-issue/8243/2 "2023-06-07T11:41:02Z")

</div>

AFAIK, compilers only support a single PCH per TU. Docs could probably be clearer here.

Cc: @brad.king

---

<div class="post-metadata">

### Author: ![magisu](https://discourse.cmake.org/user_avatar/discourse.cmake.org/magisu/32/3260_2.png) [@magisu](https://discourse.cmake.org/u/magisu)
#### Post date: [June 7, 2023, 6:26pm UTC](https://discourse.cmake.org/t/cmake-precompile-header-issue/8243/3 "2023-06-07T18:26:27Z")

</div>

Okay. So in these certain criteria, it is not possible to use a PCH to accelerate the build, right?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 7, 2023, 7:47pm UTC](https://discourse.cmake.org/t/cmake-precompile-header-issue/8243/4 "2023-06-07T19:47:52Z")

</div>

Only headers which present a consistent preprocessor state for your source is suitable. PCH files tend to be given via `-include`. And even if there are multiple such files, this flag injects the contents at the beginning of the TU (so they effectively act as a single PCH as far as your source files are concenred).

---

<div class="post-metadata">

### Author: ![magisu](https://discourse.cmake.org/user_avatar/discourse.cmake.org/magisu/32/3260_2.png) [@magisu](https://discourse.cmake.org/u/magisu)
#### Post date: [June 7, 2023, 8:40pm UTC](https://discourse.cmake.org/t/cmake-precompile-header-issue/8243/5 "2023-06-07T20:40:12Z")

</div>

gotcha. Thanks for the explanation!
