Undefined reference to 'cv:: ... '

I recently built OpenCV 4.2.0 on my XNX and i am trying to use it in a CUDA-C++ file as shown below.

#include <cudnn.h>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>

cv::Mat load_image(const char* image_path){
    cv::Mat image = cv::imread(image_path);
    image.convertTo(image, CV_32FC3);
    cv::normalize(image, image, 0, 1, cv::NORM_MINMAX);
    return image;
}

int main(void){
    
    cudnnHandle_t cudnn;
    cudnnCreate(&cudnn);

    cv::Mat image = load_image("conure.jpg");
    return 0;
}

However, when i try to compile with nvcc i get this


a bunch of undefined references to 'cv:: … ’

my path to opencv is
'/usr/include/opencv2'

and when i built OpenCV i included the flag
-DCUDNN_INCLUDE_DIR='/usr/include' \

anybody know whats wrong?, thanks.

The code seemed to compile fine, but you are missing libs for linking. You would add something like this to your link command:

-lcudnn  -lopencv_core  -lopencv_highgui

Hi, thanks for responding, how would i access this “link comand” ? where exactly do i need to enter that command you suggested? Thanks!

You would try something like:

export OPENCV_DIR=/usr/local
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$OPENCV_DIR/lib
/usr/local/cuda/bin/nvcc -ccbin g++ -std=c++11 -Xcompiler -std=c++11 testMostafa.cu -I/usr/local/cuda/include -I$OPENCV_DIR/include/opencv4  -gencode arch=compute_72,code=compute_72 -lcudnn -L$OPENCV_DIR/lib -lopencv_core -lopencv_imgcodecs -lstdc++  -o testMostafa