Pregunta

I tried a sample opencv program to run on cuda. I have doownloaded opencv and compiled it and as last step run make install and it went successfull.

I tried the following program

#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/gpu/gpu.hpp"

int main (int argc, char* argv[])
{
    try
    {
        cv::Mat src_host = cv::imread("file.png", CV_LOAD_IMAGE_GRAYSCALE);
        cv::gpu::GpuMat dst, src;
        src.upload(src_host);

        cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);

        cv::Mat result_host = dst;
        cv::imshow("Result", result_host);
        cv::waitKey();
    }
    catch(const cv::Exception& ex)
    {
        std::cout << "Error: " << ex.what() << std::endl;
    }
    return 0;
}

Saved this as test.cu and compiled. The output is

# nvcc test.cu
/tmp/tmpxft_000018a4_00000000-13_test.o: In function `main':
tmpxft_000018a4_00000000-1_test.cudafe1.cpp:(.text+0x53): undefined reference to `cv::imread(std::string const&, int)'
tmpxft_000018a4_00000000-1_test.cudafe1.cpp:(.text+0xf7): undefined reference to `cv::gpu::GpuMat::upload(cv::Mat const&)'
tmpxft_000018a4_00000000-1_test.cudafe1.cpp:(.text+0xfc): undefined reference to `cv::gpu::Stream::Null()'
tmpxft_000018a4_00000000-1_test.cudafe1.cpp:(.text+0x130): undefined reference to `cv::gpu::threshold(cv::gpu::GpuMat const&, cv::gpu::GpuMat&, double, double, int, cv::gpu::Stream&)'
....................
.....................
.......................

Then I run

# nvcc test.cu -lopencv_gpu
/usr/bin/ld: /tmp/tmpxft_000018df_00000000-13_test.o: undefined reference to symbol '_ZTIN2cv9ExceptionE'
/usr/bin/ld: note: '_ZTIN2cv9ExceptionE' is defined in DSO /usr/local/lib/libopencv_core.so.2.4 so try adding it to the linker command line
/usr/local/lib/libopencv_core.so.2.4: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status

and

# nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2012 NVIDIA Corporation
Built on Thu_Apr__5_00:24:31_PDT_2012
Cuda compilation tools, release 4.2, V0.2.1221

UPDATE

# nvcc test.cu -lm -lopencv_core -lopencv_highgui
/tmp/tmpxft_00001c51_00000000-13_test.o: In function `main':
tmpxft_00001c51_00000000-1_test.cudafe1.cpp:(.text+0xfc): undefined reference to `cv::gpu::Stream::Null()'
tmpxft_00001c51_00000000-1_test.cudafe1.cpp:(.text+0x130): undefined reference to `cv::gpu::threshold(cv::gpu::GpuMat const&, cv::gpu::GpuMat&, double, double, int, cv::gpu::Stream&)'
collect2: error: ld returned 1 exit status

I gave

# nvcc test.cu -lm -lopencv_core -lopencv_highgui -lopencv_gpu

and its working fine.

Now I m getting following error when running

# ./a.out 
OpenCV Error: Gpu API call (invalid configuration argument) in call, file /home/cuda/helloworld/Downloads/OpenCV/opencv-2.4.5/modules/gpu/include/opencv2/gpu/device/detail/transform_detail.hpp, line 361
Error: /home/cuda/helloworld/Downloads/OpenCV/opencv-2.4.5/modules/gpu/include/opencv2/gpu/device/detail/transform_detail.hpp:361: error: (-217) invalid configuration argument in function call
¿Fue útil?

Solución

Add " -lm -lopencv_core -lopencv_highgui " options to your comile code

Otros consejos

I think you got error because of library linking error.So try

As

nvcc test.cu `pkg-config --cflags --libs opencv` -lopencv_gpu

Or

nvcc test.cu -lm -lopencv_core -lopencv_highgui -lopencv_gpu. 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top