Repository navigation
Description
In one of our cc_library targets the .o is not linked against one of the libraries that is specified in its dependencies.
The missing dependency is a target within an external repository with its own BUILD file imported as an http_archive. Even though it is not linked, the external library is seen by Bazel as a dependency and is build before compiling and linking our own cc_library.
.o and dependency building and linking error observed:
>>>>> # @protobuf//:protoc_lib [action 'Linking external/protobuf/libprotoc_lib.so']
cd C:/tools/msys64/var/tmp/Bazel/k0k4tDzq/execroot/everything
SET PATH=C:/tools/msys64/usr/bin
C:/tools/msys64/usr/bin/gcc -shared -o bazel-out/local-fastbuild/bin/external/protobuf/libprotoc_lib.so -Wl,-S -Wl,@bazel-out/local-fastbuild/bin/external/protobuf/libprotoc_lib.so-2.params
>>>>> # @protobuf//:protobuf [action 'Linking external/protobuf/libprotobuf.so']
cd C:/tools/msys64/var/tmp/Bazel/k0k4tDzq/execroot/everything
SET PATH=C:/tools/msys64/usr/bin
C:/tools/msys64/usr/bin/gcc -shared -o bazel-out/local-fastbuild/bin/external/protobuf/libprotobuf.so -Wl,-S -Wl,@bazel-out/local-fastbuild/bin/external/protobuf/libprotobuf.so-2.params
>>>>> # //Proto/google/protobuf:descriptor_proto_cc [action 'Compiling Proto/google/protobuf/descriptor.pb.cc']
cd C:/tools/msys64/var/tmp/Bazel/k0k4tDzq/execroot/everything
SET PATH=C:/tools/msys64/usr/bin
C:/tools/msys64/usr/bin/gcc -std=gnu++0x -MD -MF bazel-out/local-fastbuild/bin/Proto/google/protobuf/_objs/descriptor_proto_cc/Proto/google/protobuf/descriptor.pb.d -frandom-seed=bazel-out/local-fastbuild/bin/Proto/google/protobuf/_objs/descriptor_proto_cc/Proto/google/protobuf/descriptor.pb.o -iquote . -iquote bazel-out/local-fastbuild/genfiles -iquote external/protobuf -iquote bazel-out/local-fastbuild/genfiles/external/protobuf -iquote external/bazel_tools -iquote bazel-out/local-fastbuild/genfiles/external/bazel_tools -isystem external/protobuf/src -isystem bazel-out/local-fastbuild/genfiles/external/protobuf/src -isystem external/bazel_tools/tools/cpp/gcc3 -Ibazel-out/local-fastbuild/genfiles/Proto -c bazel-out/local-fastbuild/genfiles/Proto/google/protobuf/descriptor.pb.cc -o bazel-out/local-fastbuild/bin/Proto/google/protobuf/_objs/descriptor_proto_cc/Proto/google/protobuf/descriptor.pb.o
>>>>> # //Proto/improbable:entity_state_proto_cc [action 'Compiling Proto/improbable/entity_state.pb.cc']
cd C:/tools/msys64/var/tmp/Bazel/k0k4tDzq/execroot/everything
SET PATH=C:/tools/msys64/usr/bin
C:/tools/msys64/usr/bin/gcc -std=gnu++0x -MD -MF bazel-out/local-fastbuild/bin/Proto/improbable/_objs/entity_state_proto_cc/Proto/improbable/entity_state.pb.d -frandom-seed=bazel-out/local-fastbuild/bin/Proto/improbable/_objs/entity_state_proto_cc/Proto/improbable/entity_state.pb.o -iquote . -iquote bazel-out/local-fastbuild/genfiles -iquote external/protobuf -iquote bazel-out/local-fastbuild/genfiles/external/protobuf -iquote external/bazel_tools -iquote bazel-out/local-fastbuild/genfiles/external/bazel_tools -isystem external/protobuf/src -isystem bazel-out/local-fastbuild/genfiles/external/protobuf/src -isystem external/bazel_tools/tools/cpp/gcc3 -Ibazel-out/local-fastbuild/genfiles/Proto -c bazel-out/local-fastbuild/genfiles/Proto/improbable/entity_state.pb.cc -o bazel-out/local-fastbuild/bin/Proto/improbable/_objs/entity_state_proto_cc/Proto/improbable/entity_state.pb.o
>>>>> # //Cpp/ProtocGenFabric:code_generator [action 'Linking Cpp/ProtocGenFabric/libcode_generator.so']
cd C:/tools/msys64/var/tmp/Bazel/k0k4tDzq/execroot/everything
SET PATH=C:/tools/msys64/usr/bin
C:/tools/msys64/usr/bin/gcc -shared -o bazel-out/local-fastbuild/bin/Cpp/ProtocGenFabric/libcode_generator.so -Wl,-S -Wl,@bazel-out/local-fastbuild/bin/Cpp/ProtocGenFabric/libcode_generator.so-2.params
>>>>> # //Proto/improbable:entity_state_proto_cc [action 'Linking Proto/improbable/libentity_state_proto_cc.so']
cd C:/tools/msys64/var/tmp/Bazel/k0k4tDzq/execroot/everything
SET PATH=C:/tools/msys64/usr/bin
C:/tools/msys64/usr/bin/gcc -shared -o bazel-out/local-fastbuild/bin/Proto/improbable/libentity_state_proto_cc.so -fPIC -Wl,-S -Wl,@bazel-out/local-fastbuild/bin/Proto/improbable/libentity_state_proto_cc.so-2.params
ERROR: C:/Projects/everything/Proto/improbable/BUILD:16:1: Couldn't build file Proto/improbable/libentity_state_proto_cc.so: Linking of rule '//Proto/improbable:entity_state_proto_cc' failed: gcc failed: error executing command C:/tools/msys64/usr/bin/gcc -shared -o bazel-out/local-fastbuild/bin/Proto/improbable/libentity_state_proto_cc.so -Wl,-S ... (remaining 1 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
bazel-out/local-fastbuild/bin/Proto/improbable/_objs/entity_state_proto_cc/Proto/improbable/entity_state.pb.o:entity_state.pb.cc:(.text+0x39): undefined reference to `google::protobuf::DescriptorPool::generated_pool()'
bazel-out/local-fastbuild/bin/Proto/improbable/_objs/entity_state_proto_cc/Proto/improbable/entity_state.pb.o:entity_state.pb.cc:(.text+0x39): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `google::protobuf::DescriptorPool::generated_pool()'
bazel-out/local-fastbuild/bin/Proto/improbable/_objs/entity_state_proto_cc/Proto/improbable/entity_state.pb.o:entity_state.pb.cc:(.text+0x48): undefined reference to `google::protobuf::DescriptorPool::FindFileByName(std::string const&) const'
bazel-out/local-fastbuild/bin/Proto/improbable/_objs/entity_state_proto_cc/Proto/improbable/entity_state.pb.o:entity_state.pb.cc:(.text+0x48): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `google::protobuf::DescriptorPool::FindFileByName(std::string const&) const'
[...]
The content of the bazel-out/local-fastbuild/bin/Proto/improbable/libentity_state_proto_cc.so-2.params file:
bazel-out/local-fastbuild/bin/Proto/improbable/_objs/entity_state_proto_cc/Proto/improbable/entity_state.pb.o
-lstdc++
Our cc_library target:
proto_cc_library(
name = "entity_state_proto_cc",
src = "entity_state.proto",
proto_deps = [
"//Proto/google/protobuf:descriptor.proto",
],
proto_root = "Proto",
visibility = ["//visibility:public"],
deps = [
"//Proto/google/protobuf:descriptor_proto_cc",
],
)
The proto_cc_library rule is defined as follows and as shown it implicitly adds @protobuf//:protobuf_lite as a dependency to the final cc_library.
def proto_cc_library(name, src, proto_root, proto_paths = [], proto_deps = [],
deps = [], visibility = []):
[...]
protobuf_lib = str(Label("@protobuf//:protobuf_lite"))
[...]
native.cc_library(
name = name,
hdrs = cc_hdrs,
srcs = cc_srcs,
copts = ["-I$(GENDIR)/" + proto_path for proto_path in proto_paths],
deps = [protobuf_lib] + deps,
visibility = visibility,
)
The external library defined in our WORKSPACE:
http_archive(
name = "protobuf",
url = "https://github.com/google/protobuf/archive/v3.0.0-beta-2.zip",
sha256 = "f7df9a584e2f09c93e8397c905f9c0d85a0cc698c179a134983ba5146419ad5c",
strip_prefix = "protobuf-3.0.0-beta-2",
)
Activity
meteorcloudy commentedon Oct 14, 2016
The msys toolchain is not well supported, can you try compiling with MSVC? You just need to add
--cpu=x64_windows_msvc, you can add it into ~/.bazelrc for convenience.Helcaraxan commentedon Oct 14, 2016
Oh sure had already specified it in some build scripts but not yet the one producing this error.
Specifying
--cpu=x64_windows_msvcchanges the error but now I don't even get to linking. I'm investigating and will get back here once I've drilled down to the bottom of it.Helcaraxan commentedon Oct 14, 2016
So it looks like this did solve my issue as my build passes fine now (except for another Windows-related bug).
NB: The other bug concerns the
coptsofcc_*targets. As per the Bazel docs I prefer using thecoptsto include private libraries that are not required by depending targets. However this means I need to specify things likeThis conflicts with the MSVC syntax which requires
/Iinstead of-Ito provide include paths.Should I file this as a separate issue or do you have an existing undocumented solution?
petemounce commentedon Oct 16, 2016
Ah, I'm about to have this problem too, I think.
meteorcloudy commentedon Oct 17, 2016
I think you'll have to use
selectandaction_configto specify different option for different platform.Just like:
Helcaraxan commentedon Oct 17, 2016
Okay. That should work indeed.
Nonetheless I am of the opinion that this should be fixed with a different mechanism as it does seem to me like it's going to be an issue that users will run into more and more as Bazel improves it's Windows targeting. Developers that come with it might want to make straightforward multi-platform builds.
The current proposed solution / work-around will involve a significant amount of near duplicate code in BUILD files.
meteorcloudy commentedon Oct 17, 2016
Yes, I agree. Ideally, this should be a feature inside the CROSSTOOL file, just like how we add options for normal include paths.
petemounce commentedon Oct 18, 2016
@meteorcloudy that workaround with
selectworks for me and I can make progress. I'd like the workaround to be unnecessary ;)petemounce commentedon Oct 24, 2016
@laszlocsomor #1945 sounds related? Is that file (
bazel/tools/cpp/wrapper/bin/pydir/msvc_link.py
Line 40 in 088bbc6
-I->/Icommand line switch prefix transform between non msvc and msvc...?petemounce commentedon Oct 24, 2016
Or https://github.com/bazelbuild/bazel/blob/master/tools/cpp/wrapper/bin/pydir/msvc_cl.py#L44 rather?
meteorcloudy commentedon Oct 25, 2016
Just thought about it.
If you want to add path to private headers, you can just use
includeattribute of cc_library.But there's some small difference between using
coptsandinclude, Please see https://www.bazel.io/versions/master/docs/be/c-cpp.html#cc_binary.includesHelcaraxan commentedon Nov 15, 2016
@meteorcloudy From what I know and its semantics the
includesattribute is not an option as it not private but public.hlopko commentedon Dec 2, 2016
Hello everybody :)
Please help me get up to speed. So the problem we're solving is that it's hard to express dependency on private headers in a platform independent way? Could you create a separate issue for that as it is quite disconnected from the original one. I'd be interested in how often and how many such private headers do you include. Depending on that different designs come to mind.
Thanks!
Helcaraxan commentedon Dec 2, 2016
Done. -> #2172