Skip to content

On Windows: 'cc_library' does not link against library specified in 'deps' #1944

Closed

Description

@Helcaraxan

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

  1. meteorcloudy commented on Oct 14, 2016

    @meteorcloudy
    Member

    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.

  2. Helcaraxan commented on Oct 14, 2016

    @Helcaraxan
    Author

    Oh sure had already specified it in some build scripts but not yet the one producing this error.

    Specifying --cpu=x64_windows_msvc changes 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.

  3. Helcaraxan commented on Oct 14, 2016

    @Helcaraxan
    Author

    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 copts of cc_* targets. As per the Bazel docs I prefer using the copts to include private libraries that are not required by depending targets. However this means I need to specify things like

    cc_library(
      name = "foo",
      srcs = [...],
      copts = [
        "-I<path-to-private-header>"
      ],
    )
    

    This conflicts with the MSVC syntax which requires /I instead of -I to provide include paths.

    Should I file this as a separate issue or do you have an existing undocumented solution?

  4. petemounce commented on Oct 16, 2016

    @petemounce
    Contributor

    Ah, I'm about to have this problem too, I think.

  5. meteorcloudy commented on Oct 17, 2016

    @meteorcloudy
    Member

    I think you'll have to use select and action_config to specify different option for different platform.
    Just like:

    config_setting(
        name = "windows",
        values = {"cpu": "x64_windows_msvc"},
        visibility = ["//visibility:public"],
    )
    
    select({
            "//:windows": [
                  "/I<path-to-private-header>",
            ],
           "//conditions:default": [
                "-I<path-to-private-header>",
            ],
    })
    
  6. Helcaraxan commented on Oct 17, 2016

    @Helcaraxan
    Author

    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.

  7. meteorcloudy commented on Oct 17, 2016

    @meteorcloudy
    Member

    Yes, I agree. Ideally, this should be a feature inside the CROSSTOOL file, just like how we add options for normal include paths.

  8. petemounce commented on Oct 18, 2016

    @petemounce
    Contributor

    @meteorcloudy that workaround with select works for me and I can make progress. I'd like the workaround to be unnecessary ;)

  9. petemounce commented on Oct 24, 2016

    @petemounce
    Contributor

    @laszlocsomor #1945 sounds related? Is that file (

    (r'-Wl,(.+)\.lib', ['$0.lib']),
    ) where one might define a -I -> /I command line switch prefix transform between non msvc and msvc...?

  10. meteorcloudy commented on Oct 25, 2016

    @meteorcloudy
    Member

    Just thought about it.
    If you want to add path to private headers, you can just use include attribute of cc_library.
    But there's some small difference between using copts and include, Please see https://www.bazel.io/versions/master/docs/be/c-cpp.html#cc_binary.includes

  11. Helcaraxan commented on Nov 15, 2016

    @Helcaraxan
    Author

    @meteorcloudy From what I know and its semantics the includes attribute is not an option as it not private but public.

  12. hlopko commented on Dec 2, 2016

    @hlopko
    Member

    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!

  13. Helcaraxan commented on Dec 2, 2016

    @Helcaraxan
    Author

    Done. -> #2172

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions