Bazel: Compile a single file without linking

Question

In ninja, I can compile a single C++ file by running ninja path/to/my/object.file.o.

Is there a way to achieve the same in bazel?

Use case / Background

During refactoring, in particular when changing interfaces in .hpp files, I usually want to focus on one single complex user of the interface first. I want to iterate on that one user until my refactoring works as expected on complex_user.cpp and I am happy with the new interfaces. Only afterwards, I want to adjust all other users. I hence want to get the compiler errors / warnings only from my complex_user.cpp file while ignoring all other places where .hpp might be included


Try --save_temps. bazel build --save_temps //my:library will give you the .o, .s, and similar files for only the targets listed on the command line.

--compile_one_dependency is designed for a similar use case, if you want to specify the target to build by the .cpp file instead of specifying a particular cc_library.


You need to implement a custom-made rule cc_object_file. Since the Bazel cc_rules are open source you can use this as a starting point.