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!