Coder Social home page Coder Social logo

cmake-pch's Introduction

Demonstration of using interface targets to encapsulate common header files and support exposing them as PCH components.

Use Case

Say you have a large library with many very common includes that routinely slow down your build times.

Rather than every down-stream target manually adding you as a list of includes to their PCH definition, you can either create a "OurPCH.h" or you can do everything in one place (the build system) and use cmake's interface libraries as a property bag to make these things inheritable.

a- Add the PCH files as PUBLIC/INTERFACE to a target, b- link the target privately to upstream targets.

Doing the experiment

My standard recommended build method is:

> cmake -G Ninja -DCMAKE_VERBOSE_MAKEFILE=ON -S . -B build
> cmake --build ./build

Review the top-level CMakeLists.txt file, and note the commented-out line:

# But uncomment this to make the executable also use config.h in its pch.
#target_link_libraries (my-exe project-config-pch)

Build the project as-is, then ook for cmake_pch.hxx usually in build/CMakeFiles/my-exe.dir, and note it has one include.

Now edit the top-level CMakeLists.txt file, and uncomment the target_link_libraries pch line:

# But uncomment this to make the executable also use config.h in its pch.
target_link_libraries (my-exe project-config-pch)

Save and rebuild, now revisit my-exe/cmake_pch.hxx. You'll see that config.h was inherited up into the PCH file list for the executable.

What did we see here, then?

lib1 privately associated with project-config-pch, so that connection was not exposed to my-exe; therefore despite linking to lib1, myexe does not automatically get config.h as a pch component. When we then added the association directly to myexe, we also gained the pch component.

cmake-pch's People

Contributors

kfsone avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.