rust-lang / cc-rs

Rust library for build scripts to compile C/C++ code into a Rust library
https://docs.rs/cc
Apache License 2.0
1.81k stars 434 forks source link

error: linking with `cc` failed: exit status: 1 #1006

Closed chenlvzhou closed 2 months ago

chenlvzhou commented 6 months ago
 cc::Build::new()
        .cpp(true)
        .warnings(true)
        .flag("-Wall")
        .flag("-std=c++14")
        .flag("-c")
        .files(["cpp_src/sorting.cpp", "cpp_src/print.cpp"])
        .compile("sorting");                 
        error: linking with `cc` failed: exit status: 1
  |
  = note: env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET LC_ALL="C" PATH="/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/bin:/Users/mac/.cargo/bin:/Users/mac/software/anaconda3/condabin:/Users/mac/software/apache-maven/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/usr/local/go/bin" VSLANG="1033" ZERO_AR_DATE="1" "cc" "-arch" "x86_64" "-m64" "/var/folders/1l/2z44fqw54rb2fztm8nk_0t2w0000gn/T/rustck33v2F/symbols.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.1pxhp3o9rtxxrxmg.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.1tg1n1hwo6j88wgy.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.2d3iqpbqldef27iz.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.2dkclog6uyd8qahj.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.3abpx8lt5d9ohof3.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.445p9yoszwkg5914.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.4nsqznq8pageah3j.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.4q8e8cefm8ipxqik.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.56j97f27rkka71kq.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.s1p2zarxq8bsnav.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.yzs1ijr2apqy1rs.rcgu.o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf.403mriweq4m37mka.rcgu.o" "-L" "/Users/mac/study/rustdemo/target/debug/deps" "-L" "/Users/mac/study/rustdemo/target/debug/build/rustdemo-9515264eef5f7eaa/out" "-L" "/Users/mac/study/rustdemo/target/debug/build/rustdemo-9515264eef5f7eaa/out" "-L" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-lhello" "-lsorting" "-lc++" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-4b3c9502f998a9c9.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-53709cde6aa4a693.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libobject-1494d77bb023321a.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libmemchr-68a50e7561e64f6e.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-111bf54042d9c9b3.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libgimli-2fc45f03240cf281.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-aa7833ff327b252f.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd_detect-d99d527ee281d1c7.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-cc55eed8d39cd8fc.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-c6547d77ecacc8da.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libminiz_oxide-9959121601c10901.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libadler-a6adcc42a6c760f5.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-133909f66e1d85ed.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-e3e353dbd0bf39e3.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-f33d75ee38b367b8.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-968bc4f3f78458d4.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-f8bf7097e7997f38.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-4406d769bde4f34a.rlib" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-1ebaaaec5ccf50e8.rlib" "-lSystem" "-lc" "-lm" "-L" "/Users/mac/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-o" "/Users/mac/study/rustdemo/target/debug/deps/rustdemo-743f6d5e588be2bf" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: ld: Undefined symbols:
            _print_message, referenced from:
                rustdemo::print_message_cpp::h0a6a34a8e6eaa652 in rustdemo-743f6d5e588be2bf.s1p2zarxq8bsnav.rcgu.o
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: aborting due to 1 previous error; 1 warning emitted

Merge multiple cpp files and compile them into one .a file

NobodyXu commented 6 months ago

= note: ld: Undefined symbols: _print_message, referenced from: rustdemo::print_message_cpp::h0a6a34a8e6eaa652 in rustdemo-743f6d5e588be2bf.s1p2zarxq8bsnav.rcgu.o

It seems that there's an undefined symbols in your crate rustdemo

workingjubilee commented 6 months ago

@chenlvzhou Please provide enough code to reproduce your error, otherwise we will not be able to determine how to fix this.

ds1sqe commented 3 months ago

@chenlvzhou what do "cpp_src/sorting.cpp", "cpp_src/print.cpp" contain? and show me the rust code which calling them

chenlvzhou commented 2 months ago

resolved

NobodyXu commented 2 months ago

That's good to hear!