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()

完整的错误堆栈跟踪:

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

这是导致 make 失败的代码片段:

        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();

注意 imgCPU->prop.width, imgCPU->prop.height 是整数并且

imgCPU->data[0]uint8_t *.

我应该在 CMakeLists.txt 文件中进行哪些更改才能使我的程序正确编译?

这是我的 CMakeLists.txt 文件的内容:

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")

你的 CMakeLists.txt 是错误和混乱的试试这个:

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)

删除 INCLUDE_DIRECTORIESLINK_DIRECTORIES,因为它在 modern CMake.

中已过时(错误)