Amit Jain
Amit Jain

Reputation: 241

Build C++ code with Circular dependency using Bazel Build System

In my hunt for a build system that can handle multiple languages and build code in incremental manner, I found Bazel to be a very good solution. However, there is one issue I do not have a solution for.

I have C++ code with circular dependencies. Until it is resolved, I need a build system that can build such code. My question is, can Bazel build code with circular dependencies, especially C++ code? If yes, can someone please share the guidelines or some useful links for the same?

Upvotes: 3

Views: 1841

Answers (1)

Ulf Adams
Ulf Adams

Reputation: 1329

It depends on how exactly your circular dependency looks like.

  1. Two .cc files depend on each other's .h files

    Either put both .cc and .h files into the same cc_library rule, or use header-only rules for the .h files and have the cc_library's for the .cc files depend on the corresponding other cc_library.

  2. Two .h files that #include each other

    These would both need to be in the same cc_library for Bazel to be able to handle it.

    Alternatively, if you have include guards, then you could refactor like this: Let's say the files are a.h and b.h. You'd rename b.h to b-impl.h, remove the #include for a.h from b-impl.h, and add a new file b.h that #includes a.h. That'll make the inclusion order always consistent, and remove the cycle while being mostly backwards compatible (as long as they don't both try to declare the same symbols and the code including them was dependent on the order).

  3. Two .cc files that #include each other

    Err... I hope that's not your case.

  4. Two .a files with mutual symbol references

    This is usually handled with --start-group a.a b.a --end-group, but there is no mechanism in Bazel for that right now. See https://github.com/bazelbuild/bazel/issues/818.

Upvotes: 5

Related Questions