Link cuda to rust error

 = note: /home/cery/workspace/duck_plotter/duck_plotter/target/debug/deps/duck_plotter-f93f106965cf8cad.4uyjisg3s5qfcw7q.rcgu.o: In function `duck_plotter::main':
          /home/cery/workspace/duck_plotter/duck_plotter/src/main.rs:39: undefined reference to `check_gpu'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_shabal256.cu.o): In function `Check_Cuda_Status()':
          /home/cery/workspace/duck_plotter/cuda_shabal256/cuda_shabal256.cu:7: undefined reference to `cudaGetDeviceCount'
          /home/cery/workspace/duck_plotter/cuda_shabal256/cuda_shabal256.cu:11: undefined reference to `cudaGetDeviceProperties'
          /home/cery/workspace/duck_plotter/cuda_shabal256/cuda_shabal256.cu:21: undefined reference to `cudaGetLastError'
          /home/cery/workspace/duck_plotter/cuda_shabal256/cuda_shabal256.cu:21: undefined reference to `cudaGetLastError'
          /home/cery/workspace/duck_plotter/cuda_shabal256/cuda_shabal256.cu:21: undefined reference to `cudaGetErrorString'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_shabal256.cu.o): In function `__cudaUnregisterBinaryUtil()':
          /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/host_runtime.h:259: undefined reference to `__cudaUnregisterFatBinary'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_shabal256.cu.o): In function `__nv_init_managed_rt_with_module(void**)':
          /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/host_runtime.h:264: undefined reference to `__cudaInitModule'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_shabal256.cu.o): In function `__sti____cudaRegisterAll()':
          /tmp/tmpxft_000028a7_00000000-5_cuda_shabal256.cudafe1.stub.c:14: undefined reference to `__cudaRegisterLinkedBinary_49_tmpxft_000028a7_00000000_6_cuda_shabal256_cpp1_ii_4b73c76e'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_nounce.cu.o): In function `__cudaUnregisterBinaryUtil()':
          /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/host_runtime.h:259: undefined reference to `__cudaUnregisterFatBinary'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_nounce.cu.o): In function `__nv_init_managed_rt_with_module(void**)':
          /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/host_runtime.h:264: undefined reference to `__cudaInitModule'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_nounce.cu.o): In function `__device_stub__Z15generate_nouncePhmm(unsigned char*, unsigned long, unsigned long)':
          /tmp/tmpxft_0000066c_00000000-5_cuda_nounce.cudafe1.stub.c:17: undefined reference to `__cudaPopCallConfiguration'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_nounce.cu.o): In function `__device_stub__Z16swap_poc2_formatPhm(unsigned char*, unsigned long)':
          /tmp/tmpxft_0000066c_00000000-5_cuda_nounce.cudafe1.stub.c:1: undefined reference to `__cudaPopCallConfiguration'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_nounce.cu.o): In function `__device_stub__Z19clip_mem2poc_formatPhS_(unsigned char*, unsigned char*)':
          /tmp/tmpxft_0000066c_00000000-5_cuda_nounce.cudafe1.stub.c:1: undefined reference to `__cudaPopCallConfiguration'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_nounce.cu.o): In function `__device_stub__Z31generate_compressed_poc2_formatPhS_(unsigned char*, unsigned char*)':
          /tmp/tmpxft_0000066c_00000000-5_cuda_nounce.cudafe1.stub.c:1: undefined reference to `__cudaPopCallConfiguration'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_nounce.cu.o): In function `__nv_cudaEntityRegisterCallback(void**)':
          /tmp/tmpxft_0000066c_00000000-5_cuda_nounce.cudafe1.stub.c:1: undefined reference to `__cudaRegisterFunction'
          /tmp/tmpxft_0000066c_00000000-5_cuda_nounce.cudafe1.stub.c:1: undefined reference to `__cudaRegisterFunction'
          /tmp/tmpxft_0000066c_00000000-5_cuda_nounce.cudafe1.stub.c:1: undefined reference to `__cudaRegisterFunction'
          /tmp/tmpxft_0000066c_00000000-5_cuda_nounce.cudafe1.stub.c:1: undefined reference to `__cudaRegisterFunction'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_nounce.cu.o): In function `__sti____cudaRegisterAll()':
          /tmp/tmpxft_0000066c_00000000-5_cuda_nounce.cudafe1.stub.c:2: undefined reference to `__cudaRegisterLinkedBinary_46_tmpxft_0000066c_00000000_6_cuda_nounce_cpp1_ii_69bad568'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_nounce.cu.o): In function `cudaError cudaLaunchKernel<char>(char const*, dim3, dim3, void**, unsigned long, CUstream_st*)':
          /usr/local/cuda/bin/../targets/x86_64-linux/include/cuda_runtime.h:209: undefined reference to `cudaLaunchKernel'
          /home/cery/workspace/duck_plotter/duck_plotter/cuda_lib/libcuda_shabal256.a(cuda_nounce.cu.o):(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
          collect2: error: ld returned 1 exit status

The post does not seem to provide sufficient information, e.g. problem description, compiler version, compile command, crates used, code snippet that causes the problem, etc. Providing these may make it easier for others to identify problems.

From the error message it seems the linker could not find all the symbols from the CUDA library, and most of them are from libcudart . Probably you could change your linker to nvcc , which knows how to find the libs, or you could specify the path of all libraries that you need to link against to your current linker.

2 Likes

how to change link to nvcc ? i am a newer

Could you please elaborate these information?

In addition, environment variables like $LD_LIBRARY_PATH.

Without these information it's hard for others to help you identify problems. :slightly_smiling_face:

Before having these information, I could only take a guess. In most cases it's easier to specify the path of all libraries that you need to link with than to change a linker. For linkage issue, there are typically three cases:

  1. you did not specify the library to be linked with;
  2. you did not specify the path of the libraries to be searched in;
  3. both of above.

Solution for problem 1:

  • Case I: If you have some extern "C" blocks in your rust code and you could change it:

    • Change it to #[link(name = "cudart")] extern "C" {...}
  • Case II: If you would like to temporarily change your $RUSTFLAGS:

    • Use RUSTFLAGS="-lcudart" cargo build when building.
  • Case III: If you want to set this flag in build scripts, see Alternative Solution.

Solution for problem 2:

  • Case I: If you would like to temporarily change your $LD_LIBRARY_PATH:

    • Do export LD_LIBRARY_PATH="folder/of/cudalibs:$LD_LIBRARY_PATH" in you command line. Replace folder/of/cudalibs with the actually path of you cuda lib. It's likely /usr/local/cuda/lib64 on Linux.
  • Case II: If you would like to temporarily change your $RUSTFLAGS:

    • Use RUSTFLAGS="-Lfolder/of/cudalibs" cargo build when building. It can be combined with Case II for problem 1 as: RUSTFLAGS="-Lfolder/of/cudalibs -lcudart" cargo build if you want both.
  • Case III: If you want to set this flag in build scripts, see Alternative Solution.

Alternative Solution: Build scripts

  • In your Cargo.toml, add build = "build.rs" to the [package] section.

  • Add a build.rs file in your project root, and write fn main() { ... }

  • For problem 1:

    • In ..., add a line println!("cargo:rustc-link-lib=cudart");
  • For problem 2:

    • In ..., add a line println!("cargo:rustc-link-search=native=folder/of/cudalibs");. Replace folder/of/cudalibs with the actually path of you cuda lib.