Ubuntu下编译TensorFlow C++库用于Qt时Bazel报错求助
Hey there, let's work through those frustrating compilation errors you're hitting. I've dealt with similar issues before, so here's how to tackle each problem step by step:
1. The name 'set' is not defined Error in closure_css_library.bzl
This error pops up because the io_bazel_rules_closure dependency (used by TensorFlow) is using an outdated function called set() that Bazel renamed to depset() in newer versions. Here are two ways to fix it:
Update the rules_closure in your TensorFlow workspace
Open theWORKSPACEfile in your TensorFlow source folder. Look for the section whereio_bazel_rules_closureis being pulled in. Replace that entry with the latest version of rules_closure that usesdepsetinstead ofset. For example:http_archive( name = "io_bazel_rules_closure", sha256 = "d21c041f1ecc3d7f55e3a7749e96e981c9a0370c60632696147753452d5b4e87", strip_prefix = "rules_closure-0.10.0", urls = ["https://github.com/bazelbuild/rules_closure/archive/refs/tags/0.10.0.tar.gz"], )Switch to a Bazel version compatible with your TensorFlow release
TensorFlow and Bazel are tightly coupled—each TensorFlow version has a specific Bazel version it's tested with. For example:- TensorFlow 1.15.x needs Bazel 0.26.1
- TensorFlow 2.0.x works with Bazel 0.26.1 or 0.27.1
- TensorFlow 2.1.x requires Bazel 0.27.1
Check the release notes for your exact TensorFlow version, install that Bazel version, and your error should go away.
2. The bazel needs to be > 0.4.3, found 0.13.1 Version Check Error
This is a silly string comparison bug in older TensorFlow code. The check is comparing version numbers as strings instead of numbers—so "0.13.1" gets treated as less than "0.4.3" because "1" comes before "4" in the second segment. Here's how to fix it:
Patch the version check logic
Find the file in TensorFlow that handles Bazel version checks (usuallytensorflow/workspace.bzl). Look for code that looks like this:if bazel_version < "0.4.3": fail("Bazel version must be at least 0.4.3")Replace it with a proper numeric comparison:
def parse_version(v): return tuple(map(int, v.split("."))) if parse_version(bazel_version) < (0, 4, 3): fail("Bazel version must be at least 0.4.3")Again, use the recommended Bazel version
If you install the exact Bazel version that your TensorFlow release recommends, this broken check won't even be an issue—since the recommended version will definitely meet the requirement.
Quick Tips for Qt Compatibility
- Compile with the right C++ standard: Qt typically uses C++17 or later, so add
--cxxopt=-std=c++17to your Bazel build command. - Generate position-independent code: Qt apps need this, so include
--linkopt=-fPICin your build flags. - Clean thoroughly: Before retrying, run
bazel clean --expungeto wipe all cached build files—sometimes old artifacts cause weird errors.
内容的提问来源于stack exchange,提问作者kardhem

