Linker error: /usr/bin/ld: cannot find -lcudart_static while trying to compile CUDA code with clang

Linker error: /usr/bin/ld: cannot find -lcudart_static while trying to compile CUDA code with clang

我尝试按照官方文档here:

编译一个axpy.cu文件
clang++ axpy.cu -o exec --cuda-gpu-arch=sm_60 -L/usr/local/cuda -lcudart_static -ldl -lrt -pthread

但这给出了 linker 错误和警告:

clang: warning: Unknown CUDA version. cuda.h: CUDA_VERSION=11060. Assuming the latest supported version 10.1 [-Wunknown-cuda-version]

/usr/bin/ld: cannot find -lcudart_static

clang: error: linker command failed with exit code 1 (use -v to see invocation)

我安装了 clang 版本 11。 在 运行:

nvidia-smi

我得到:

我在某处读到我需要向 libcudart 文件或其他内容添加符号 link,如果有帮助的话。

我在 运行 上得到以下输出:

ld -lcudart_static --verbose
attempt to open /usr/local/lib/x86_64-linux-gnu/libcudart_static.so failed
attempt to open /usr/local/lib/x86_64-linux-gnu/libcudart_static.a failed
attempt to open /lib/x86_64-linux-gnu/libcudart_static.so failed
attempt to open /lib/x86_64-linux-gnu/libcudart_static.a failed
attempt to open /usr/lib/x86_64-linux-gnu/libcudart_static.so failed
attempt to open /usr/lib/x86_64-linux-gnu/libcudart_static.a failed
attempt to open /usr/lib/x86_64-linux-gnu64/libcudart_static.so failed
attempt to open /usr/lib/x86_64-linux-gnu64/libcudart_static.a failed
attempt to open /usr/local/lib64/libcudart_static.so failed
attempt to open /usr/local/lib64/libcudart_static.a failed
attempt to open /lib64/libcudart_static.so failed
attempt to open /lib64/libcudart_static.a failed
attempt to open /usr/lib64/libcudart_static.so failed
attempt to open /usr/lib64/libcudart_static.a failed
attempt to open /usr/local/lib/libcudart_static.so failed
attempt to open /usr/local/lib/libcudart_static.a failed
attempt to open /lib/libcudart_static.so failed
attempt to open /lib/libcudart_static.a failed
attempt to open /usr/lib/libcudart_static.so failed
attempt to open /usr/lib/libcudart_static.a failed
attempt to open /usr/x86_64-linux-gnu/lib64/libcudart_static.so failed
attempt to open /usr/x86_64-linux-gnu/lib64/libcudart_static.a failed
attempt to open /usr/x86_64-linux-gnu/lib/libcudart_static.so failed
attempt to open /usr/x86_64-linux-gnu/lib/libcudart_static.a failed
ld: cannot find -lcudart_static
attempt to open /usr/local/lib/x86_64-linux-gnu/libcudart_static.so failed
attempt to open /usr/local/lib/x86_64-linux-gnu/cudart_static.a failed
attempt to open /lib/x86_64-linux-gnu/libcudart_static.so failed
attempt to open /lib/x86_64-linux-gnu/cudart_static.a failed
attempt to open /usr/lib/x86_64-linux-gnu/libcudart_static.so failed
attempt to open /usr/lib/x86_64-linux-gnu/cudart_static.a failed
attempt to open /usr/lib/x86_64-linux-gnu64/libcudart_static.so failed
attempt to open /usr/lib/x86_64-linux-gnu64/cudart_static.a failed
attempt to open /usr/local/lib64/libcudart_static.so failed
attempt to open /usr/local/lib64/cudart_static.a failed
attempt to open /lib64/libcudart_static.so failed
attempt to open /lib64/cudart_static.a failed
attempt to open /usr/lib64/libcudart_static.so failed
attempt to open /usr/lib64/cudart_static.a failed
attempt to open /usr/local/lib/libcudart_static.so failed
attempt to open /usr/local/lib/cudart_static.a failed
attempt to open /lib/libcudart_static.so failed
attempt to open /lib/cudart_static.a failed
attempt to open /usr/lib/libcudart_static.so failed
attempt to open /usr/lib/cudart_static.a failed
attempt to open /usr/x86_64-linux-gnu/lib64/libcudart_static.so failed
attempt to open /usr/x86_64-linux-gnu/lib64/cudart_static.a failed
attempt to open /usr/x86_64-linux-gnu/lib/libcudart_static.so failed
attempt to open /usr/x86_64-linux-gnu/lib/cudart_static.a failed

有人可以帮忙吗?

所以我找到了解决办法。显然,链接器无法找到 libcudart 二进制文件。所以使用 find 来获取它的位置:

find /usr/ -name libcudart_static*

得到它的路径为:

/usr/local/cuda-11.6/targets/x86_64-linux/lib/libcudart_static.a

(您可能会有所不同)。

刚刚在编译命令中使用-L 标志链接了这个路径。 新命令:

clang++ axpy.cu -o exec --cuda-gpu-arch=sm_60 -L/usr/local/cuda -L/usr/local/cuda-11.6/targets/x86_64-linux/lib/ -lcudart_static -ldl -lrt -pthread