Linking Cuda in C++ issue

江枫思渺然 提交于 2019-12-24 09:41:53

问题


I've searched existing questions, but I can't seem to solve this.

I have a blur_mask.cc file with:

#include <iostream>
#include <ctime>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <vector_types.h>
#include <cuda.h>
#include <cuda_runtime.h>

extern "C" void gpuBlurMask(unsigned char* srcData, int srcStep, uchar3* dst, int dstStep, int width, int height, float *mask, int maskStep, int maskWidth, int maskHeight, int blockSize=16);

using namespace std;
using namespace cv;
using namespace cv::gpu;

void blurMask(Mat& src, Mat& dst, Mat& mask) {
    GpuMat gpuSrc, gpuDst, gpuMask;

    gpuSrc.upload(src);
    gpuDst.upload(dst);
    gpuMask.upload(mask);

    gpuBlurMask(gpuSrc.data, gpuSrc.step, gpuDst.ptr<uchar3>(), gpuDst.step, gpuSrc.cols, gpuSrc.rows, gpuMask.ptr<float>(), gpuMask.step, gpuMask.cols, gpuMask.rows);

    gpuDst.download(dst);    
}
...

And a gpu_blur.cu file that contains

extern "C"
void gpuBlurMask(unsigned char* srcData, int srcStep, uchar3* dst, int dstStep, int width, int height, float *mask, int maskStep, int maskWidth, int maskHeight, int blockSize=16) {
...

When I do

nvcc -c -o gpu_blur gpu_blur.cu

I get no errors, but when compiling

g++ -o blur_mask blur_mask.cc gpu_blur -I /usr/local/cuda/include/ -lopencv_core -lopencv_highgui -lopencv_gpu

I get the following errors:

 g++ -o blur_mask blur_mask.cc gpu_blur -I /usr/local/cuda/include/ -lopencv_core -lopencv_highgui -lopencv_gpu -lcuda
gpu_blur: In function `gpuBlurMask':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x59): undefined reference to `cudaMallocArray'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0xa0): undefined reference to `cudaMemcpy2DToArray'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x139): undefined reference to `cudaConfigureCall'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x16f): undefined reference to `cudaDeviceSynchronize'
gpu_blur: In function `__cudaUnregisterBinaryUtil()':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x184): undefined reference to `__cudaUnregisterFatBinary'
gpu_blur: In function `__device_stub__Z14blurMaskKernelP6uchar3iiiPfiii(uchar3*, int, int, int, float*, int, int, int)':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x1b9): undefined reference to `cudaSetupArgument'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x1dc): undefined reference to `cudaSetupArgument'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x1ff): undefined reference to `cudaSetupArgument'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x222): undefined reference to `cudaSetupArgument'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x245): undefined reference to `cudaSetupArgument'
gpu_blur:tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x264): more undefined references to `cudaSetupArgument' follow
gpu_blur: In function `__nv_cudaEntityRegisterCallback(void**)':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x381): undefined reference to `__cudaRegisterFunction'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x3af): undefined reference to `__cudaRegisterTexture'
gpu_blur: In function `__sti____cudaRegisterAll_43_tmpxft_00000905_00000000_6_gpu_blur_cpp1_ii_srcTex()':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x3c3): undefined reference to `__cudaRegisterFatBinary'
gpu_blur: In function `cudaChannelFormatDesc cudaCreateChannelDesc<unsigned char>()':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text._Z21cudaCreateChannelDescIhE21cudaChannelFormatDescv[cudaChannelFormatDesc cudaCreateChannelDesc<unsigned char>()]+0x34): undefined reference to `cudaCreateChannelDesc'
gpu_blur: In function `cudaError cudaBindTextureToArray<unsigned char, 2, (cudaTextureReadMode)0>(texture<unsigned char, 2, (cudaTextureReadMode)0> const&, cudaArray const*, cudaChannelFormatDesc const&)':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text._Z22cudaBindTextureToArrayIhLi2EL19cudaTextureReadMode0EE9cudaErrorRK7textureIT_XT0_EXT1_EEPK9cudaArrayRK21cudaChannelFormatDesc[cudaError cudaBindTextureToArray<unsigned char, 2, (cudaTextureReadMode)0>(texture<unsigned char, 2, (cudaTextureReadMode)0> const&, cudaArray const*, cudaChannelFormatDesc const&)]+0x27): undefined reference to `cudaBindTextureToArray'
gpu_blur: In function `cudaError cudaLaunch<char>(char*)':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text._Z10cudaLaunchIcE9cudaErrorPT_[cudaError cudaLaunch<char>(char*)]+0x14): undefined reference to `cudaLaunch'
collect2: ld returned 1 exit status

Thank you.


回答1:


You need to link to cudart library (-L/usr/local/cuda/lib64 -lcudart)



来源:https://stackoverflow.com/questions/17151941/linking-cuda-in-c-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!