# Automatic Object Linkage, with Include Graphs

DevFeed: [Automatic Object Linkage, with Include Graphs](<https://devfeed.tech/articles/automatic-object-linkage-with-include-graphs-28618.md>)

Original publisher: [Read original article](<https://upcoder.com/19/automatic-object-linkage-with-include-graphs>)

Published: 2018-05-31T13:33:00Z

Content type: article

Language: en

Sources: [Thomas Young](<https://devfeed.tech/sources/thomas-young.md>)

Topics: [Compilers and Linkers](<https://devfeed.tech/topics/compilers-and-linkers.md>), [linker](<https://devfeed.tech/topics/linker.md>), [static linking](<https://devfeed.tech/topics/static-linking.md>), [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [Test-driven development](<https://devfeed.tech/topics/tdd.md>), [test impact analysis](<https://devfeed.tech/topics/test-impact-analysis.md>)

Tags: [3d](<https://devfeed.tech/tags/3d.md>), [application](<https://devfeed.tech/tags/application.md>), [automatic](<https://devfeed.tech/tags/automatic.md>), [automatically](<https://devfeed.tech/tags/automatically.md>), [based](<https://devfeed.tech/tags/based.md>), [build](<https://devfeed.tech/tags/build.md>), [c](<https://devfeed.tech/tags/c.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [code-sharing](<https://devfeed.tech/tags/code-sharing.md>), [compilation](<https://devfeed.tech/tags/compilation.md>), [content](<https://devfeed.tech/tags/content.md>), [custom](<https://devfeed.tech/tags/custom.md>), [dependencies](<https://devfeed.tech/tags/dependencies.md>), [dll](<https://devfeed.tech/tags/dll.md>), [graphs](<https://devfeed.tech/tags/graphs.md>), [include](<https://devfeed.tech/tags/include.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [modular](<https://devfeed.tech/tags/modular.md>), [multiple](<https://devfeed.tech/tags/multiple.md>), [object](<https://devfeed.tech/tags/object.md>)

## AI overview

The article describes a custom build system that derives each target's link inputs from include relationships: including a header with a matching source file implies a dependency on that source, and analysis expands recursively from one or more root files. This can share object files across targets and produce leaner application and test builds than broad static library groupings, reducing manual project setup and recompilation. The approach assumes disciplined header/source pairs and headers that reflect actual use; bypassed headers can cause missing links, while headers with mostly inline code can pull in unnecessary objects. PathEngine disallows conditional include directives so headers can be analyzed once, but adopting the method required code changes and a custom build system. IDE integration trades off features: external build projects support dynamic targets and shared objects but can lose IntelliSense, while generated projects retain IDE features but do not update dynamically and do not share objects.

## Source excerpt

In this post I describe an alternative approach to sharing source code elements between multiple build targets. It's based on a custom build process I implemented at PathEngine for our C and C++ code-base (but the core ideas could also be relevant to other languages with compilation to object files). We'll need to enforce some constraints on the way the source code is set up, notably with regards to header file organisation, but can then automatically determine what to include in the link operation for each build target, based on a graph of include relationships extracted from the source files. The resulting dynamic, fine grained, object-file centric approach to code sharing avoids the need for problematic static library decomposition, and the associated 'false dependencies'. Motivation We need to work with a bunch of different build targets, at PathEngine. There's a run-time pathfinding dll, for example, but also a 3D content processing dll, a 3D testbed application, and so on, with a lot of source code shared between these different targets. The code was originally split into a set of (internal) static libraries, for this purpose. based on things like theme (e.g. 'Geometry.lib') or application layer ('PathEngine_Core.lib', 'PathEngine_Interface.lib'). Different targets could then share common source code by sharing these static libraries. This worked, as far as it goes, but something didn't feel right. The libraries weren't particularly modular, for one thing, and just felt like big old bags of object files that didn't really reflect the actual structure of the underlying code. A bigger problem was that, for any kind of prototyping work, I ended up having to link in a big chunk of the PathEngine source code, even where only some small part of this was really required. Unit testing was also difficult to setup, for a similar reason. Since throwing out the static libraries and switching to an alternative approach (as described in this post), prototyping and unit test