Home > Software engineering >  make error: undefined reference to cpp_redis::client::set(..) and cv::Mat::~Mat()
make error: undefined reference to cpp_redis::client::set(..) and cv::Mat::~Mat()

Time:05-31

Complete error stack trace:

undefined reference to `cv::Mat::Mat(int, int, int, void*, unsigned long)'
undefined reference to `cv::imencode(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
undefined reference to `cpp_redis::client::client()'
undefined reference to `cpp_redis::client::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, cpp_redis::connect_state)> const&, unsigned int, int, unsigned int)'
undefined reference to `cpp_redis::client::set(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
undefined reference to `cpp_redis::client::sync_commit()'
undefined reference to `cpp_redis::client::~client()'
undefined reference to `cv::Mat::~Mat()'
undefined reference to `cpp_redis::client::~client()'
undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
src/sensors/camera/camera/CMakeFiles/sample_camera.dir/build.make:130: recipe for target 'src/sensors/camera/camera/sample_camera' failed
make[2]: *** [src/sensors/camera/camera/sample_camera] Error 1
CMakeFiles/Makefile2:2246: recipe for target 'src/sensors/camera/camera/CMakeFiles/sample_camera.dir/all' failed
make[1]: *** [src/sensors/camera/camera/CMakeFiles/sample_camera.dir/all] Error 2
Makefile:149: recipe for target 'all' failed
make: *** [all] Error 2

Here is the code snippet which is causing make to fail:

        std::vector<uchar> buf; 
        cv::Mat matImage = cv::Mat(imgCPU->prop.width, imgCPU->prop.height, CV_8UC3, imgCPU->data[0]);
        cv::imencode(".jpg", matImage, buf);
        
        cpp_redis::client client;
        client.connect();
        client.set("image", {buf.begin(), buf.end()});
        client.sync_commit();

Note that imgCPU->prop.width, imgCPU->prop.height are ints and

imgCPU->data[0] is uint8_t *.

What changes should I make in the CMakeLists.txt file for my program to compile correctly?

Here is what my CMakeLists.txt file looks:

project(sample_camera C CXX)
pkg_check_modules(OPENCV opencv)
if(${OPENCV_FOUND})
MESSAGE("OPENCV_FOUND:" ${OPENCV_FOUND})
MESSAGE("OPENCV_VERSION:" ${OPENCV_VERSION})
MESSAGE("OPENCV_LIBRARIES:" ${OPENCV_LIBRARIES})
MESSAGE("OPENCV_INCLUDE_DIRS:" ${OPENCV_INCLUDE_DIRS})
MESSAGE("OPENCV_LIBRARY_DIRS:" ${OPENCV_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${OPENCV_INCLUDE_DIRS})
INCLUDE_DIRECTORIES("/usr/include/opencv4")
INCLUDE_DIRECTORIES("/usr/include/opencv4/opencv2")
LINK_DIRECTORIES(${OPENCV_LIBRARY_DIRS})
LINK_DIRECTORIES("/usr/include/opencv4/opencv2")
endif()
#-------------------------------------------------------------------------------
# Project files
#-------------------------------------------------------------------------------
set(PUBLIC_DOCS
    README.md
)

set(SOURCES
    main.cpp
)

set(LIBRARIES
    ${Driveworks_LIBRARIES}
    samples_framework
    lodepng-src
)

#-------------------------------------------------------------------------------
# Final target
#-------------------------------------------------------------------------------

add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBRARIES})
set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER "Samples")

# ------------------------------------------------------------------------------
# Install target
# ------------------------------------------------------------------------------
sdk_add_sample(${PROJECT_NAME})
sdk_add_sample_data(${PROJECT_NAME} "samples/sensors/camera/camera")

CodePudding user response:

Your CMakeLists.txt is wrong and messy try this:

cmake_minimum_required(VERSION 3.16)
project(DisplayImage CXX)

find_package(OpenCV REQUIRED COMPONENTS core imgproc video)

...
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} 
    PRIVATE
        opencv_core 
        opencv_video
        opencv_imgproc)

Drop INCLUDE_DIRECTORIES and LINK_DIRECTORIES since it is obsolete (wrong) in modern CMake.

  • Related