OpenCV / CUDA linking on Jetson Nano

I am developing a solution that includes blob detection. As far as I know, most of the functionality I will need to perform the heavy-duty calculations on the GPU is already implemented in OpenCV. But I also need to figure out how to transfer custom computation routines, in case I ever need that, on the GPU through CUDA. To that end I created the most simple testing program I could think of: all it’s supposed to do is call an empty CUDA kernel function that’s defined, together with its wrapper, in a .cu file. The wrapper is also declared in a header file both the .cu file and the .cpp file include. The problem is, g++ can’t seem to find the OpenCV libraries I include in the .cpp file (so as to ensure I can use my custom CUDA code together with the OpenCV functionality).

Before, I have already compiled and launched a small CUDA sample program with nvcc, and OpenCV-using sample programs of varying complexity with CMake and Make. What I need now is a way to use both custom CUDA kernels and OpenCV in one program.

cudaPart.h:

void kernelWrapper();

cudaPart.cu:

#include <cuda_runtime.h>
#include "cudaPart.h"

// Empty kernel function
__global__ void kernelTest()
{
}

// Wrapper function
void kernelWrapper()
{
	kernelTest<<<1, 1>>>();
}

openCVPart.cpp

#include <opencv2/opencv.hpp>
#include "cudaPart.h"

using namespace std;

int main(int argc, char** argv)
{
	kernelWrapper();
	
	cout << "CUDA link test successful" << endl;

	return 0;
}

Makefile

all: openCVPart.o gpuCode.o
	g++ gpuCode.o cudaPart.o openCVPart.o -lcudart -o testApp

gpuCode.o: cudaPart.o
	nvcc -dlink cudaPart.o -o gpuCode.o

cudaPart.o:
	nvcc cudaPart.cpp -o cudaPart.o

openCVPart.o:
	g++ -c openCVPart.cpp -o openCVPart.o

My guess is I’m missing something crucial in Makefile that tells the compiler where exactly do the files I’m trying to include reside, or that I’m using the wrong compiler in g++ altogether. I tried referencing the Makefile that CMake created that I use in my working OpenCV sample, but couldn’t figure out a single thing from it. And probably I made other mistakes in the Makefile: I mean, this is the first time I code in Linux and build stuff by hand rather than let an IDE handle all the dirty work. So I’d like some kind of a pointer as to how exactly do I build this.

You might need to link against openCV libraries that you are using . Something like -lopencv_core

1 Like

Hi @trueshadow97

As @neel_patel stated you might need to link against OpenCV library,

You can get required flags with the following command:

 pkg-config --libs opencv

Or if you are using OpenCV > 4.0.0

pkg-config --libs opencv4  

You will get an output similar to the following:

nvidia@nvidia:~$ pkg-config --libs opencv4
-lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_video -lopencv_cudawarping -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
3 Likes

Hi trueshadow97

Complementing the replies above, I would package the flags by using also:

# For the linker
pkg-config --libs opencv4  
# For the compiler
pkg-config --cflags opencv4  

In your makefile, I would add:

Adding the corresponding flags and libs will depend on where you use OpenCV or other libraries.

Hope this help you.

Regards,
Leon.

2 Likes