Cuda 5.0链接问题
我只是想使用cuda 5.0 Preview构建我的旧项目. 链接时出现错误,告诉我某些cuda函数找不到.例如:
I'm just trying to build an old project of mine using cuda 5.0 preview. I get an Error when linking, telling me that certain cuda functions can not be found. For example:
undefined reference to 'cudaMalloc'
.
我的链接命令包括cuda的以下选项:
My linking command includes the following options for cuda :
-L/usr/local/cuda/lib64 -L/home/myhome/NVIDIA_CUDA_Samples/C/lib -L /home/myhome/NVIDIA_CUDA_Samples/C/common/lib/linux -lcudart
-L/usr/local/cuda/lib64 -L/home/myhome/NVIDIA_CUDA_Samples/C/lib -L/home/myhome/NVIDIA_CUDA_Samples/C/common/lib/linux -lcudart
ls -lah /usr/local/cuda/lib64/
给了我8个cuda库,包括libcudart.so.5.0.7和仅使用.so-file-end的符号链接.
ls -lah /usr/local/cuda/lib64/
gives me 8 cuda libraries including libcudart.so.5.0.7 with symlinks using only the .so-file-ending.
ls /home/myhome/NVIDIA_CUDA_Samples/C/lib/
给我一个空目录,这有点奇怪吗?
ls /home/myhome/NVIDIA_CUDA_Samples/C/lib/
gives me an empty directory, which is kind of strange?
ls /home/myhome/NVIDIA_CUDA_Samples/C/common/lib/linux/
给了我两个目录:i686和x86_64都只包含libGLEW.a
ls /home/myhome/NVIDIA_CUDA_Samples/C/common/lib/linux/
gives me two directories: i686 and x86_64 both containing only libGLEW.a
我不知道该寻找一种解决方案.感谢您的帮助!
I have no idea which way to look for a solution. Any help is appreciated!
这是我完整的链接命令(TARGET_APPLICATION是我的二进制文件,x86_64/Objectfiles.o代表所有(23)个目标文件,包括用nvcc编译的目标文件):
Here is my complete linking command (TARGET_APPLICATION is my binary and x86_64/Objectfiles.o stands for all (23) object files including the object file compiled with nvcc):
/home/myhome/nullmpi-0.7/bin/mpicxx -CC=g++ -I. -I/home/myhome/nullmpi-0.7/src -I/usr/lib/openmpi/include -L/usr/local/cuda/lib64 -L/home/myhome/NVIDIA_CUDA_Samples/C/lib -L/home/myhome/NVIDIA_CUDA_Samples/C/common/lib/linux -lcudart -o TARGET_APPLICATION x86_64/Objectfiles.o /usr/lib/liblapack.so /usr/lib/libblas.so /home/myhome/nullmpi-0.7/lib/libnullpmpi.a -lm
我使用 nullmpi 进行编译和链接(项目使用MPI和CUDA),内部使用g++
,如-CC=g++
所示,我想保留这些内容.
I use nullmpi for compilation and linking (project uses MPI and CUDA), which internally uses g++
as can be seen by -CC=g++
, i wanted to keep this stuff out.
我的cuda目标文件的编译命令:
The compilation command for my cuda object file:
/usr/local/cuda/bin/nvcc -c -arch=sm_21 -L/home/myhome/NVIDIA_CUDA_Samples/C/lib -O3 kernelwrapper.cu -o x86_64/kernelwrapper.RELEASE.2.o
echo $LD_LIBRARY_PATH
结果为:
/usr/local/cuda/lib64:/usr/local/cuda/lib:
echo $PATH
结果为:
otherOptions
:/usr/local/cuda/bin:/home/myhome/nullmpi-0.7/bin
我正在构建64位.为了完整起见,我在Ubuntu 12.04上构建. (64位). 构建CUDA示例很好.
I'm building 64-bit. For the sake of completeness I'm building on Ubuntu 12.04. (64bit). Building the CUDA Samples works fine.
解决方案(感谢为我指出这一点的爪子):
SOLUTION (thanks to talonmies for pointing me to it):
这是正确的链接命令:
/home/myhome/nullmpi-0.7/bin/mpicxx -CC=g++ -I. -I/home/myhome/nullmpi-0.7/src -I/usr/lib/openmpi/include -L/usr/local/cuda/lib64 -L/home/myhome/NVIDIA_CUDA_Samples/C/lib -L/home/myhome/NVIDIA_CUDA_Samples/C/common/lib/linux -o TARGET_APPLICATION x86_64/Objectfiles.o /usr/lib/liblapack.so /usr/lib/libblas.so /home/myhome/nullmpi-0.7/lib/libnullpmpi.a -lcudart -lm
您的链接语句顺序错误.应该更像这样:
You have your linking statements in the incorrect order. It should be something more like this:
/home/myhome/nullmpi-0.7/bin/mpicxx -CC=g++ -I. -I/home/myhome/nullmpi-0.7/src \
-I/usr/lib/openmpi/include -L/usr/local/cuda/lib64 \
-L/home/myhome/NVIDIA_CUDA_Samples/C/lib \
-L/home/myhome/NVIDIA_CUDA_Samples/C/common/lib/linux \
-o TARGET_APPLICATION x86_64/Objectfiles.o \
/home/myhome/nullmpi-0.7/lib/libnullpmpi.a -llapack -lblas -lm -lcudart
问题的根源是您已在包含该对象的依赖关系的对象文件中之前指定了CUDA运行时库.链接器只是从链接中丢弃libcudart.so
,因为在处理该点时对它没有任何依赖关系. POSIX样式编译语句中的黄金法则:链接语句从左到右进行解析;因此对象首先包含外部依赖关系,然后满足这些依赖关系的库.
The source of your problem is that you have specified the CUDA runtime library before the object file that contains a dependency to it. The linker simply discards libcudart.so
from the linkage because there are no dependencies to it at the point when it is processed. Golden rule in POSIX style compilation statements: linkage statements are parsed left-to-right; so objects containing external dependencies first, libraries satisfying those dependencies afterwards.