我使用catkin编译CUDA项目时遇到了一个我认为是链接器错误的问题。下面是源代码的片段。当我尝试运行catkin build
时出现错误:error
In function `curand_init(double*, int, int):
./src/cwru_cuda.cu undefined reference to `curandCreateGenerator'
./src/cwru_cuda.cu undefined reference to `curandSetPseudoRandomGeneratorSeed'
我检查了详细的生成输出,它使用c++链接文件,它传递了适当的标志,就像我在CURAND_FLAGS
中定义的那样。我不确定这里可能还有什么问题?我对构建工具不是很了解。CURAND reference
cwru_cuda.h
#include <cuda.h>
#include <cuda_runtime.h>
#include <curand.h>
#include <helper_cuda.h>
#include <device_launch_parameters.h>
void curand_init(double*, int, int);
cwru_cuda.cu
#include <cwru_cuda.h>
curandGenerator_t __curand_gen;
double* __curand_float_address;
size_t __curand_float_size;
int __curand_num_elems;
void curand_init(double* address, int numElements, int seed = 1234) {
curandCreateGenerator(&__curand_gen, CURAND_RNG_PSEUDO_DEFAULT);
curandSetPseudoRandomGeneratorSeed(__curand_gen, seed);
__curand_num_elems = numElements;
__curand_float_size = numElements * sizeof(double);
cudaMallocManaged(&__curand_float_address, __curand_float_size);
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(cwru_cuda)
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
)
# https://stackoverflow.com/questions/25748039/add-cuda-to-ros-package
find_package(CUDA REQUIRED)
# set CUDA_NVCC_FLAGS as you would do with CXX/C FLAGS
set(CUDA_NVCC_FLAGS CACHE STRING "nvcc flags" FORCE)
set(CUDA_VERBOSE_BUILD ON CACHE BOOL "nvcc verbose" FORCE)
set(CURAND_FLAGS "-lcurand_static -lculibos -lcudart_static -lpthread -ldl -I /usr/local/cuda/include -L /usr/local/cuda/lib64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CURAND_FLAGS}")
set(LIB_TYPE STATIC)
cuda_add_library(cuda_vector_lib ${LIB_TYPE} src/cwru_cuda.cu)
catkin_package(
INCLUDE_DIRS include
LIBRARIES cuda_vector_lib
# CATKIN_DEPENDS roscpp std_msgs
# DEPENDS system_lib
)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_executable(cuda_vector_test src/tests.cpp src/Timer.cpp)
add_dependencies(cuda_vector_test cuda_vector_lib)
TARGET_LINK_LIBRARIES(cuda_vector_test
${catkin_LIBRARIES}
cuda_vector_lib
)
用catkin build cwru_cuda --verbose
构建build output
make[2]: Entering directory '/home/ethan/catkin_ws/build/cwru_cuda'
[ 60%] Linking CXX executable /home/ethan/catkin_ws/devel/.private/cwru_cuda/lib/cwru_cuda/cuda_vector_test
/usr/bin/cmake -E cmake_link_script CMakeFiles/cuda_vector_test.dir/link.txt --verbose=1
/usr/bin/c++ -lcurand_static -lculibos -lcudart_static -lpthread -ldl -I /usr/local/cuda/include -L /usr/local/cuda/lib64 -rdynamic CMakeFiles/cuda_vector_test.dir/src/tests.cpp.o CMakeFiles/cuda_vector_test.dir/src/Timer.cpp.o -o /home/ethan/catkin_ws/devel/.private/cwru_cuda/lib/cwru_cuda/cuda_vector_test -Wl,-rpath,/opt/ros/melodic/lib /opt/ros/melodic/lib/libroscpp.so -lboost_filesystem /opt/ros/melodic/lib/librosconsole.so /opt/ros/melodic/lib/librosconsole_log4cxx.so /opt/ros/melodic/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/melodic/lib/libxmlrpcpp.so /opt/ros/melodic/lib/libroscpp_serialization.so /opt/ros/melodic/lib/librostime.so /opt/ros/melodic/lib/libcpp_common.so -lboost_system -lboost_thread -lboost_chrono -lboost_date_time -lboost_atomic -lpthread /usr/lib/x86_64-linux-gnu/libconsole_bridge.so.0.4 libcuda_vector_lib.a /usr/local/cuda-11.0/lib64/libcudart_static.a -lpthread -ldl -lrt
libcuda_vector_lib.a(cuda_vector_lib_generated_cwru_cuda.cu.o): In function `curand_init(double*, int, int)':
/home/ethan/catkin_ws/src/cwru_cuda/src/cwru_cuda.cu:23: undefined reference to `curandCreateGenerator'
/home/ethan/catkin_ws/src/cwru_cuda/src/cwru_cuda.cu:24: undefined reference to `curandSetPseudoRandomGeneratorSeed'
libcuda_vector_lib.a(cuda_vector_lib_generated_cwru_cuda.cu.o): In function `generate_uniform_double()':
/home/ethan/catkin_ws/src/cwru_cuda/src/cwru_cuda.cu:31: undefined reference to `curandGenerateUniformDouble'
collect2: error: ld returned 1 exit status
CMakeFiles/cuda_vector_test.dir/build.make:141: recipe for target '/home/ethan/catkin_ws/devel/.private/cwru_cuda/lib/cwru_cuda/cuda_vector_test' failed
最佳答案
Robert Crovella在上面的comments中回答了这个问题。
我能够使用target_link_libraries()
来设置满足依赖性的-lcurand
和-lcublas
。所以我将以下行添加到了CMakeLists.txt
target_link_libraries(cuda_vector_lib -lcublas -lcurand)