I’ve been continuing to read Professional CMake, and there’s a section within that book about precompiled headers.
I’m trying to wrap my head around how to best utilize these precompiled headers in my project. To help visualize, I created a theoretical example for this post.
In terms of layout, you certainly would not organize your project like this, but the point is to narrow down which files should be added as precompiled headers.
This is a contrived example, so there’s no HAL or anything like that, but as you can see above, the GPIO driver is used from various other libraries/drivers. At a first glance, this would seem like a good candidate for a precompiled header…I think?
If that’s the correct assumption, then the question is, what would actually live in that precompiled header, and where would it live?
For example, would I declare it at the root level CMakeLists.txt, or would I declare it within the GPIO directory as part of its CMakeLists.txt?
Similarly, what would go in the pch.h file? Would I include only the outward-facing header files exposed by the GPIO interface? Kind of like this:
target_precompile_headers(gpio PUBLIC gpio.h gpio_registers.h gpio_driver.h)
Another option would be to create a “common” interface library containing the precompiled headers for the entire project, and then link the dependent libs to that. They actually did this in the book, which is where I got the idea,
For example, if there was a common lib, it might look like this:
# Common PCH lib for all dependent libs
add_library(common INTERFACE)
# Assume that we added gpio.h into pch.h
target_precompile_headers(common PUBLIC pch.h)
Then, in my other libs, I could do something like:
# Within each app's CMakeLists.txt
target_link_libraries(foo INTERFACE common gpio uart)
target_precompile_headers(foo REUSE_FROM common)
target_link_libraries(bar INTERFACE common spi uart)
target_precompile_headers(bar REUSE_FROM common)
target_link_libraries(baz INTERFACE common gpio)
target_precompile_headers(baz REUSE_FROM common)
# Within each respective driver's CMakeLists.txt
target_link_libraries(uart INTERFACE common gpio)
target_precompile_headers(uart REUSE_FROM common)
target_link_libraries(spi INTERFACE common gpio)
target_precompile_headers(spi REUSE_FROM common)
# for main within it's CMakeLists.txt
target_link_libraries(main INTERFACE common gpio spi uart foo bar baz)
target_precompile_headers(main REUSE_FROM common)
Is that the right idea, or am I off in the weeds on this one.
Thanks!