Bazel项目引入带子目录头文件的ArduinoJson报未声明包含错误
It looks like the issue stems from a mismatch between how your Bazel cc_library is configured for ArduinoJson and how your code includes the headers—even though you’ve already added the dependency to your Serialization target. Let’s break down the fixes step by step:
1. Simplify the includes Configuration
Your current includes list adds both the root src directory and the nested ArduinoJson subdirectory, which creates ambiguity for Bazel’s dependency resolver. Since ArduinoJson’s headers are designed to be included as <ArduinoJson/Subdirectory/File.hpp>, you only need to add the root source directory to includes:
cc_library( name = "ArduinoJson", hdrs = glob(["5.12.0/src/**/*.hpp", "5.12.0/src/**/*.h"]), includes = [ "5.12.0/src", # Only this root path is required ], visibility = ["//visibility:public"], )
This tells the compiler to look for ArduinoJson/... starting from 5.12.0/src, which matches the library’s standard include pattern.
2. Verify Header Inclusion Syntax in Your Code
Make sure your JsonDeserializer.cpp uses angle-bracket includes relative to the library’s root, not absolute or file-system relative paths. For example:
// Correct (Bazel will associate this with the ArduinoJson dependency) #include <ArduinoJson/Data/Encoding.hpp> #include <ArduinoJson/Serialization/FloatParts.hpp> // Incorrect (Bazel treats this as a direct file dependency, not part of the library) #include "../../Third-Party/ArduinoJson/5.12.0/src/ArduinoJson/Data/Encoding.hpp"
Using relative paths makes Bazel fail to recognize the files as part of the //Third-Party/ArduinoJson dependency, triggering the undeclared inclusion error.
3. (Optional) Add strip_include_prefix for Future-Proofing
If you want to avoid tying your code to the version number (5.12.0) in the include path, add strip_include_prefix to the ArduinoJson cc_library:
cc_library( name = "ArduinoJson", hdrs = glob(["5.12.0/src/**/*.hpp", "5.12.0/src/**/*.h"]), includes = ["5.12.0/src"], strip_include_prefix = "5.12.0/src", # Removes the versioned path from include resolution visibility = ["//visibility:public"], )
This lets you keep the same <ArduinoJson/...> include syntax even if you update the library version later, no source code changes needed.
4. Confirm Glob Coverage
The glob(["5.12.0/src/**/*.hpp", "5.12.0/src/**/*.h"]) ensures you’re only including header files (not any stray non-header files in the src tree). This helps Bazel’s dependency analysis focus on the correct files.
After making these changes, rebuild your Serialization target. Bazel should now correctly associate the included headers with the //Third-Party/ArduinoJson dependency and resolve the undeclared inclusion errors.
内容的提问来源于stack exchange,提问作者Anders Sundman

