Skip to content

What is a proper way to build cc_binary #1441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dunnock opened this issue Feb 12, 2025 · 1 comment
Open

What is a proper way to build cc_binary #1441

dunnock opened this issue Feb 12, 2025 · 1 comment

Comments

@dunnock
Copy link

dunnock commented Feb 12, 2025

When I m trying to build cc_binary with Bazel I cannot find how to properly specify dependencies so that linking would work. For instance if compiling provided demo from this repository (see dunnock#1 for full diff):

cc_binary(
    name = "democc",
    srcs = ["src/main.cc"],
    copts = ["-std=c++17"],
    deps = [
        ":blobstore-sys",
        ":blobstore-include",
        ":bridge",
        "//:cxx",
        "//:core",
    ],
)

I get linking errors:

ERROR: /home/maxim/src/rust/cxx/demo/BUILD.bazel:16:10: Linking demo/democc failed: (Exit 1): linux-sandbox failed: error executing CppLink command 
  (cd /home/maxim/.cache/bazel/_bazel_maxim/9a0a14cdd72bc9df5400d22d4d657a3e/sandbox/linux-sandbox/50/execroot/_main && \
  exec env - \
    PATH=/home/maxim/.cache/bazelisk/downloads/sha256/40f243b118f46d1c88842315e78ec5f9f6390980d67a90f7b64098613e60d65b/bin:/opt/intel/oneapi/vtune/2023.2.0/bin64:/opt/intel/oneapi/vtune/latest/bin64/:/usr/local/cuda/bin:/home/maxim/.local/bin:/opt/intel/oneapi/vtune/2023.2.0/bin64:/opt/intel/oneapi/vtune/latest/bin64/:/usr/local/cuda/bin:/home/maxim/.cargo/bin:/home/maxim/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/usr/local/go/bin:/home/maxim/go/bin:/usr/local/go/bin:/home/maxim/go/bin \
    PWD=/proc/self/cwd \
    TMPDIR=/tmp \
    ZERO_AR_DATE=1 \
  /home/maxim/.cache/bazel/_bazel_maxim/install/3fd603d608c2d3b856ea3d982b11227c/linux-sandbox -t 15 -w /dev/shm -w /home/maxim/.cache/bazel/_bazel_maxim/9a0a14cdd72bc9df5400d22d4d657a3e/sandbox/linux-sandbox/50/execroot/_main -w /tmp -M /home/maxim/.cache/bazel/_bazel_maxim/9a0a14cdd72bc9df5400d22d4d657a3e/sandbox/linux-sandbox/50/_hermetic_tmp -m /tmp -S /home/maxim/.cache/bazel/_bazel_maxim/9a0a14cdd72bc9df5400d22d4d657a3e/sandbox/linux-sandbox/50/stats.out -D /home/maxim/.cache/bazel/_bazel_maxim/9a0a14cdd72bc9df5400d22d4d657a3e/sandbox/linux-sandbox/50/debug.out -- /usr/bin/gcc @bazel-out/k8-fastbuild/bin/demo/democc-0.params)
bazel-out/k8-fastbuild/bin/demo/_objs/bridge/main.rs.pic.o:main.rs.cc:function org::blobstore::MultiBuf::layout::size(): error: undefined reference to 'org$blobstore$cxxbridge1$MultiBuf$operator$sizeof'
bazel-out/k8-fastbuild/bin/demo/_objs/bridge/main.rs.pic.o:main.rs.cc:function org::blobstore::MultiBuf::layout::align(): error: undefined reference to 'org$blobstore$cxxbridge1$MultiBuf$operator$alignof'
bazel-out/k8-fastbuild/bin/demo/_objs/bridge/main.rs.pic.o:main.rs.cc:function org::blobstore::create_multibuf(rust::cxxbridge1::Vec<unsigned char>): error: undefined reference to 'org$blobstore$cxxbridge1$create_multibuf'
bazel-out/k8-fastbuild/bin/demo/_objs/bridge/main.rs.pic.o:main.rs.cc:function org::blobstore::next_chunk(org::blobstore::MultiBuf&): error: undefined reference to 'org$blobstore$cxxbridge1$next_chunk'
bazel-out/k8-fastbuild/bin/demo/_objs/bridge/main.rs.pic.o:main.rs.cc:function rust::cxxbridge1::Box<org::blobstore::MultiBuf>::allocation::alloc(): error: undefined reference to 'cxxbridge1$box$org$blobstore$MultiBuf$alloc'
bazel-out/k8-fastbuild/bin/demo/_objs/bridge/main.rs.pic.o:main.rs.cc:function rust::cxxbridge1::Box<org::blobstore::MultiBuf>::allocation::dealloc(org::blobstore::MultiBuf*): error: undefined reference to 'cxxbridge1$box$org$blobstore$MultiBuf$dealloc'
bazel-out/k8-fastbuild/bin/demo/_objs/bridge/main.rs.pic.o:main.rs.cc:function rust::cxxbridge1::Box<org::blobstore::MultiBuf>::drop(): error: undefined reference to 'cxxbridge1$box$org$blobstore$MultiBuf$drop'
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o:cxx.cc:function rust::cxxbridge1::String::String(): error: undefined reference to 'cxxbridge1$string$new'
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o:cxx.cc:function rust::cxxbridge1::String::String(rust::cxxbridge1::String const&): error: undefined reference to 'cxxbridge1$string$clone'
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o:cxx.cc:function rust::cxxbridge1::String::String(rust::cxxbridge1::String&&): error: undefined reference to 'cxxbridge1$string$new'
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o:cxx.cc:function rust::cxxbridge1::String::~String(): error: undefined reference to 'cxxbridge1$string$drop'
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o:cxx.cc:function rust::cxxbridge1::initString(rust::cxxbridge1::String*, char const*, unsigned long): error: undefined reference to 'cxxbridge1$string$from_utf8'
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o:cxx.cc:function rust::cxxbridge1::initString(rust::cxxbridge1::String*, char16_t const*, unsigned long): error: undefined reference to 'cxxbridge1$string$from_utf16'
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o:cxx.cc:function rust::cxxbridge1::String::String(rust::cxxbridge1::String::lossy_t, char const*, unsigned long): error: undefined reference to 'cxxbridge1$string$from_utf8_lossy'
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o:cxx.cc:function rust::cxxbridge1::String::String(rust::cxxbridge1::String::lossy_t, char16_t const*, unsigned long): error: undefined reference to 'cxxbridge1$string$from_utf16_lossy'
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o:cxx.cc:function rust::cxxbridge1::String::operator=(rust::cxxbridge1::String const&) &: error: undefined reference to 'cxxbridge1$string$drop'
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o:cxx.cc:function rust::cxxbridge1::String::operator=(rust::cxxbridge1::String const&) &: error: undefined reference to 'cxxbridge1$string$clone'
...

contents of democc-0.params:

❯ cat bazel-out/k8-fastbuild/bin/demo/democc-0.params
-o
bazel-out/k8-fastbuild/bin/demo/democc
-Wl,-S
-fuse-ld=gold
-B/usr/bin
-Wl,-no-as-needed
-Wl,-z,relro,-z,now
-pass-exit-codes
bazel-out/k8-fastbuild/bin/demo/_objs/democc/main.pic.o
-Wl,--start-lib
bazel-out/k8-fastbuild/bin/demo/_objs/blobstore-sys/blobstore.pic.o
-Wl,--end-lib
-Wl,--start-lib
bazel-out/k8-fastbuild/bin/demo/_objs/bridge/main.rs.pic.o
-Wl,--end-lib
bazel-out/k8-fastbuild/bin/libcxx-3687376307.a
-Wl,--start-lib
bazel-out/k8-fastbuild/bin/_objs/core-lib/cxx.pic.o
-Wl,--end-lib
bazel-out/k8-fastbuild/bin/external/+crate_repositories+vendor__foldhash-0.1.4/libfoldhash-1264529756.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libtest-3f2fb5628a902976.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-5742b2ff1aee27a6.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-0ca9d0e07c79bf1c.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_std-cc5e90924cebe413.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-057643b1ee86c6e4.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-766a7f754c8a7b1e.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-bea85df8985bb2f0.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-a6ffb752c82ede63.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgetopts-faba618726e33e66.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-042a1cb2143833ef.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-9b015ab0e459320f.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-c896c1f3ebd992a5.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-d03b432324732a0e.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-8f13995dfe88b143.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-8e63f34add4b5f76.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libproc_macro-53bdffff3ca9feef.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libprofiler_builtins-f63320f165eccbac.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-092847c11df9843e.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libsysroot-bee2d06ba2b291cb.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunicode_width-cb7e1a26da719554.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-b3cefab73e1d8804.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-45742ba23c5ac84b.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-7edbe936f6124018.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-1e6496089ac34c68.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-3d4809363f767eb8.a
bazel-out/k8-fastbuild/bin/external/rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-915fd1ebf7e3c376.a
-Wl,--start-lib
bazel-out/k8-fastbuild/bin/external/rules_rust+/ffi/cc/allocator_library/_objs/allocator_library/allocator_library.pic.o
-Wl,--end-lib
-ldl
-lpthread
-Wl,--push-state,-as-needed
-lstdc++
-Wl,--pop-state
-Wl,--push-state,-as-needed
-lm
-Wl,--pop-state

@dunnock
Copy link
Author

dunnock commented Feb 12, 2025

I figured it out, the bridge is exporting definitions but not the implementations, to make cc linker to pick implementations from Rust those also should be exported as rust_static_library, e.g

 rust_static_library(
    name = "demolib",
    srcs = ["src/main.rs"],
    edition = "2021",
    deps = [
        ":blobstore-sys",
        ":bridge",
        "//:cxx",
    ],
) 

I have implemented this in my linked PR, let me know if that is ok to amend demo that way or is it better to create another democc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant