【发布时间】:2017-04-04 21:41:46
【问题描述】:
当我使用 Cmake 编译此代码时.. 这个错误已显示给我.... 我试图删除CUDA并重新安装它,但它是一样的
#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core/cuda.hpp"
#include "opencv2/cudaimgproc.hpp"
#include "opencv2/cudafilters.hpp" // cv::cuda::Filter
#include "opencv2/cudaarithm.hpp" // cv::cuda::abs orcv::cuda::addWeighted
#include "timer.h"
using namespace std;
using namespace cv;
void processUsingOpenCvCpu(std::string nput_file, std::string output_file);
void processUsingOpenCvGpu(std::string input_file, std::string output_file);
void processUsingCuda(std::string input_file, std::string output_file);
int main(int argc, char **argv) {
const string input_file = argc >= 2 ? argv[1] : "evarest.jpg";
const string output_file_OpenCvCpu = argc >= 3 ? argv[2] : "output_OpenCvCpu.jpg";
const string output_file_OpenCvGpu = argc >= 4 ? argv[3] : "output_OpenCvGpu.jpg";
const string output_file_Cuda = argc >= 5 ? argv[2] : "output_Cuda.jpg";
for (int i=0; i<5; ++i) {
processUsingOpenCvCpu(input_file, output_file_OpenCvCpu);
processUsingOpenCvGpu(input_file, output_file_OpenCvGpu);
//processUsingCuda(input_file, output_file_Cuda);
}
return 0;
}
void processUsingOpenCvGpu(std::string input_file, std::string output_file) {
//Read input image from the disk
Mat input = imread(input_file, CV_LOAD_IMAGE_COLOR);
Mat output;
if(input.empty())
{
std::cout<<"Image Not Found: "<< input_file << std::endl;
return;
}
GpuTimer timer;
timer.Start();
// copy the input image from CPU to GPU memory
cuda::GpuMat gpuInput = cuda::GpuMat(input);
// blur the input image to remove the noise
Ptr<cv::cuda::Filter> filter = cv::cuda::createGaussianFilter(gpuInput.type(), gpuInput.type(), Size(3,3), 0);
filter->apply(gpuInput, gpuInput);
// convert it to grayscale (CV_8UC3 -> CV_8UC1)
cv::cuda::GpuMat gpuInput_gray;
cv::cuda::cvtColor( gpuInput, gpuInput_gray, COLOR_RGB2GRAY );
// compute the gradients on both directions x and y
cv::cuda::GpuMat gpuGrad_x, gpuGrad_y;
cv::cuda::GpuMat abs_gpuGrad_x, abs_gpuGrad_y;
int scale = 1;
int ddepth = CV_16S; // use 16 bits unsigned to avoid overflow
// gradient x direction
filter = cv::cuda::createSobelFilter(gpuInput_gray.type(), ddepth, 1, 0, 3, scale, BORDER_DEFAULT);
filter->apply(gpuInput_gray, gpuGrad_x);
cv::cuda::abs(gpuGrad_x, gpuGrad_x);
gpuGrad_x.convertTo(abs_gpuGrad_x, CV_8UC1); // CV_16S -> CV_8U
// gradient y direction
filter = cv::cuda::createSobelFilter(gpuInput_gray.type(), ddepth, 0, 1, 3, scale, BORDER_DEFAULT);
filter->apply(gpuInput_gray, gpuGrad_y);
cv::cuda::abs(gpuGrad_y, gpuGrad_y);
gpuGrad_y.convertTo(abs_gpuGrad_y, CV_8UC1); // CV_16S -> CV_8U
// create the output by adding the absolute gradient images of each x and y direction
cv::cuda::GpuMat gpuOutput;
cv::cuda::addWeighted( abs_gpuGrad_x, 0.5, abs_gpuGrad_y, 0.5, 0, gpuOutput );
// copy the result gradient from GPU to CPU and release GPU memory
gpuOutput.download(output);
gpuOutput.release();
gpuInput.release();
gpuInput_gray.release();
gpuGrad_x.release();
gpuGrad_y.release();
abs_gpuGrad_x.release();
abs_gpuGrad_y.release();
timer.Stop();
printf("OpenCV GPU code ran in: %f msecs.\n", timer.Elapsed());
//show image
imshow("Image", output);
// wait until user press a key
waitKey(0);
//imwrite(output_file, output);
}
这是错误
[ 50%] Linking CXX executable tut1
/usr/bin/ld: CMakeFiles/tut1.dir/1.cpp.o: undefined reference to symbol 'cudaEventSynchronize'
//usr/lib/x86_64-linux-gnu/libcudart.so.7.5: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/tut1.dir/build.make:122: recipe for target 'tut1' failed
make[2]: *** [tut1] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/tut1.dir/all' failed
make[1]: *** [CMakeFiles/tut1.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
当我删除 CUDA 时......他没有识别出 CUDA 的所有功能......但现在错误只出现在 cudaEventSynchronize 上......
【问题讨论】:
-
需要修改构建系统链接CUDA运行库(libcudart)
-
感谢您的回复……但我想我已经做到了……我认为问题出在链接器中……而不是编译器……您怎么看?
-
我就是这么说的。您必须链接 CUDA 运行时库。你必须修改你正在使用的make系统来做到这一点。
-
我也遇到了同样的问题,你解决了吗?