xmake-io / xmake

🔥 A cross-platform build utility based on Lua
https://xmake.io
Apache License 2.0
9.57k stars 763 forks source link

ld cannot find related library when build cuda program #5317

Closed ArberSephirotheca closed 1 week ago

ArberSephirotheca commented 2 weeks ago

Xmake Version

xmake v2.9.3+20240703

Operating System Version and Architecture

Ubuntu 22.04, Arm64

Describe Bug

When I tried to build cuda program with xmake, the linker cannot find the related library during linking.

/usr/bin/ld: cannot find -lcudart_static: No such file or directory
/usr/bin/ld: cannot find -lcudadevrt: No such file or directory

It turns out that in arm64 architecture, the cuda libraries are stored under /usr/local/cuda/lib64 instead of /usr/local/cuda/lib. but the rpath is pointed to the later path by default, making linker unable to find the library.

Expected Behavior

The cuda program should be built successfully.

Project Configuration

add_rules("mode.debug", "mode.release")

set_languages("c++17")
set_warnings("all")
set_optimize("fastest")

add_requires("benchmark")

-- Kernels to benchmark 
target("cuda-kernels")
    set_kind("static")
    add_includedirs("$(projectdir)/include")
    add_files("src/RGBAConverter.cu")
    add_cugencodes("native")

target("bench-sycl")
    set_kind("binary")
    add_includedirs("$(projectdir)/include")
    add_files("src/main.cu")
    add_cugencodes("native")
    add_packages("benchmark")
    add_packages("oneDPL")
    add_deps("cuda-kernels")

Additional Information and Error Logs

 xmake -v
[ 87%]: linking.release bench-sycl
/usr/bin/g++ -o build/linux/arm64-v8a/release/bench-sycl build/.objs/bench-sycl/linux/arm64-v8a/release/src/main.cu.o build/.objs/bench-sycl/linux/arm64-v8a/release/rules/cuda/devlink/bench-sycl_gpucode.cu.o -L/usr/local/cuda/lib -Lbuild/linux/arm64-v8a/release -Wl,-rpath=/usr/local/cuda/lib -s -lbenchmark -lcuda-kernels -lcudart_static -lrt -lpthread -ldl -lcudadevrt
/usr/bin/ld: cannot find -lcudart_static: No such file or directory
/usr/bin/ld: cannot find -lcudadevrt: No such file or directory
collect2: error: ld returned 1 exit status
error: execv(/usr/bin/g++ -o build/linux/arm64-v8a/release/bench-sycl build/.objs/bench-sycl/linux/arm64-v8a/release/src/main.cu.o build/.objs/bench-sycl/linux/arm64-v8a/release/rules/cuda/devlink/bench-sycl_gpucode.cu.o -L/usr/local/cuda/lib -Lbuild/linux/arm64-v8a/release -Wl,-rpath=/usr/local/cuda/lib -s -lbenchmark -lcuda-kernels -lcudart_static -lrt -lpthread -ldl -lcudadevrt) failed(1)
waruqi commented 2 weeks ago

try this patch. https://github.com/xmake-io/xmake/pull/5319

xmake update -s github:xmake-io/xmake#cuda
ArberSephirotheca commented 1 week ago

@waruqi Thanks for the help! That patch does not fix, I tried to change the line 108 to

    elseif is_host("linux") and is_arch("x86_64", "arm64-v8a") then

It fixes the problem.

waruqi commented 1 week ago

Strange, why would it be arm64-v8a?

Can you run

xmake l os.arch
uname -a
xmake f -cvD
xmake -rv

and provide all logs? I need to check your machine arch.

waruqi commented 1 week ago

try it again, I have fixed arm64 arch name. https://github.com/xmake-io/xmake/pull/5319

ArberSephirotheca commented 1 week ago

@waruqi The problem now is resolved. Thanks!