Search code examples
c++visual-studio-2010

How can I make projects share intermediate (.obj) files?


I have a collection of C++ solutions. They all include a directory of common code. Not just #include, but they individually build common source files as well. Here's an example of the directory structure:

code/
    Common/
    ProjectA/
    ProjectB/

So ProjectA and ProjectB both include files from Common. I have set the intermediate directory for both projects to be code/_build/Debug/Obj.

I had expected that if I built ProjectA first, then ProjectB would not need to rebuild the intermediate files from the Commonsource. This is not happening. ProjectB is rebuilding those object files, and if I build ProjectA again, it will rebuild them as well.

It's as if a conditional rebuild will be triggered if the object file was not built by the project which is currently compiling. Is this true?

I know I could just create a library out of the common code, but this is not an option.


Solution

  • It's as if a conditional rebuild will be triggered if the object file was not built by the project which is currently compiling. Is this true?

    Yes. Each project maintains its own separate list of build files. Each project builds those files separately from every other object, because each project also maintains its own separate build options. Every project is separate from all others, even within the same solution.

    The problem you describe has been encountered before. A long time ago in fact. The solution to it was, and remains, quite simple: create a third project which creates a static library of common code, which is linked by the other two projects. Indeed, static libraries exist for precisely this reason.

    If circumstances forbid you from using the solution that was designed to solve this exact problem, then you have two choices:

    1. change those circumstances so that you can use the solution.
    2. accept the needlessly longer build times incurred by your situation.