Home > front end >  undefined reference to `cv::imread` when compiled from file other than main.cpp
undefined reference to `cv::imread` when compiled from file other than main.cpp

Time:12-28

I am unable to read or write the image when written in another file such as ImgEnt.cpp but the same piece of code is working in main.cpp when built with CMake and make

ImgEnt.cpp

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>

ImgEnt::ImgEnt(const std::string &filename, const std::string &filepath, const std::string &dirname, const std::string &output_dir) {
    this->filepath = filepath;
    this->filename = filename;
    this->output_dir = output_dir   fs::path::preferred_separator   dirname;
}

cv::Mat ImgEnt::input_image() const {
    cv::Mat input_img, output_img; // not causing issue
    cv::Size size(640, 640); // not causing issue
    input_img = cv::imread(this->filepath, cv::IMREAD_COLOR); // THIS IS CAUSING ISSUE
//    cv::resize(input_img, output_img, size);
//
//    return output_img;
    return output_img;
}

std::string ImgEnt::output_filepath() const {
    fs::path odir(this->output_dir);
    if (!exists(odir)) {
        fs::create_directories(odir);
    }

    return this->output_dir   fs::path::preferred_separator   this->filename;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.21)

# project and language settings
project(Week2 VERSION 0.1.0)
enable_language(CXX CUDA)

# c   standard and runtime directory
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

# libtorch settings
if (DEFINED ENV{CMAKE_PREFIX_PATH})
    message(STATUS "Setting the cmake prefix path to \"$ENV{CMAKE_PREFIX_PATH}\"")
    set(CMAKE_PREFIX_PATH "$ENV{CMAKE_PREFIX_PATH};${CMAKE_PREFIX_PATH};")
else ()
    message(STATUS "Setting default cmake prefix path to \"/opt/libtorch/share/cmake/Torch\"")
    set(CMAKE_PREFIX_PATH "/opt/libtorch/share/cmake/Torch;${CMAKE_PREFIX_PATH};")
endif ()

# download modal if not exists
if (NOT EXISTS ${PROJECT_SOURCE_DIR}/model)
    file(MAKE_DIRECTORY ${PROJECT_SOURCE_DIR}/model)
endif ()
if (NOT EXISTS ${PROJECT_SOURCE_DIR}/model/yolo5.pt)
    message(STATUS "Download the yolo v5 model into ${PROJECT_SOURCE_DIR}/model/yolo5.pt")
    file(DOWNLOAD https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5x.pt ${PROJECT_SOURCE_DIR}/model/yolo5.pt)
endif ()

# find packages
find_package(Torch REQUIRED)
find_package(LIBMAGIC REQUIRED)
find_package(OpenCV REQUIRED COMPONENTS opencv_core opencv_imgcodecs)
find_package(Boost REQUIRED)

# configure include directories, executable and link libraries
add_executable(${PROJECT_NAME} main.cpp ImgEnt.cpp ImgEnt.hpp)
include_directories(${Boost_INCLUDE_DIR} "${OpenCV_INCLUDE_DIRS}" "${LIBMAGIC_INCLUDES}" "${TORCH_INCLUDE_DIRS}")
target_link_libraries(${PROJECT_NAME} opencv_imgcodecs opencv_core "${TORCH_LIBRARIES}" "${LIBMAGIC_LIBRARIES}")

Build logs

$ cmake . && make
-- The C compiler identification is GNU 11.1.0
-- The CXX compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c   - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- The CUDA compiler identification is NVIDIA 11.5.119
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Check for working CUDA compiler: /opt/cuda/bin/nvcc - skipped
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
-- Setting default cmake prefix path to "/opt/libtorch/share/cmake/Torch"
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Found Torch: /opt/libtorch/lib/libtorch.so  
-- Found LIBMAGIC: /usr/lib/libmagic.so  
-- Found components for LIBMAGIC
-- LIBMAGIC_ROOT_DIR  = /usr/local
-- LIBMAGIC_INCLUDES  = /usr/include
-- LIBMAGIC_LIBRARIES = /usr/lib/libmagic.so
-- Found OpenCV: /usr (found version "4.5.4") found components: opencv_core opencv_imgcodecs 
-- Found Boost: /usr/lib64/cmake/Boost-1.76.0/BoostConfig.cmake (found version "1.76.0")  
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/Week2
[ 33%] Building CXX object CMakeFiles/Week2.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/Week2.dir/ImgEnt.cpp.o
[100%] Linking CXX executable bin/Week2
/usr/bin/ld: CMakeFiles/Week2.dir/ImgEnt.cpp.o: in function `ImgEnt::input_image() const':
ImgEnt.cpp:(.text 0x332): undefined reference to `cv::imread(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Week2.dir/build.make:120: bin/Week2] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Week2.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

I have written a similar code in the main.cpp that does the following and works fine

  1. Read image
  2. Converts to HSV and resize to 250 x 250
  3. Save in /tmp/img/processed.cpp

CodePudding user response:

Well, I had a CPU version of the OpenCV and I was compiling with Cuda enabled. After removing the old version and installing opencv-cuda. It worked and it totally makes sense to me now.

  • Related