CMake precompile header issue

I have the following header files

h1.h

#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

#ifndef __HEADER2__
#define __HEADER2__

#include <iostream>
#include <string>

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

#endif

main.cpp

#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

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

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!

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

Cc: @brad.king

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

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).

gotcha. Thanks for the explanation!