当我写这段代码时:
CascadeClassifier faceCascade;
faceCascade.load("../peripheral/cv_zone/Resources/haarcascade_frontalface_default.xml");
if (faceCascade.empty()) {
cout << "XML file not loaded" << endl;
}
我收到错误如下:
/usr/bin/ld: CMakeFiles/vedio_opencv.dir/with_opencv/src/cv_zone_Project3.cpp.o: in function `cv_zone_faceDetection()':
cv_zone_Project3.cpp:(.text+0x80): undefined reference to `cv::CascadeClassifier::CascadeClassifier()'
/usr/bin/ld: cv_zone_Project3.cpp:(.text+0xc5): undefined reference to `cv::CascadeClassifier::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: cv_zone_Project3.cpp:(.text+0xf2): undefined reference to `cv::CascadeClassifier::empty() const'
/usr/bin/ld: cv_zone_Project3.cpp:(.text+0x1bf): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
/usr/bin/ld: cv_zone_Project3.cpp:(.text+0x299): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/vedio_opencv.dir/build.make:331:/home/zero/openCV/chip_phytium/opencv_output/vedio_opencv] 错误 1
make[1]: *** [CMakeFiles/Makefile2:83:CMakeFiles/vedio_opencv.dir/all] 错误 2
但是当我跟踪 CascadeClassifier 的定义时,我可以在 objdetect.hpp 中找到它。所以我检查了我的 CMakelist.txt ,它是一样的 有人可以帮助我吗
我的CMakelist.txt文件如下:
cmake_minimum_required(VERSION 3.16.3)
Project(opencv_SDK_phytium)
option(RUNNING_OPENCV "Build with OpenCV" ON)
if(DEFINED RUNNING_OPENCV)
if(RUNNING_OPENCV)
message("Build with OpenCV")
find_package(OpenCV REQUIRED)
# opencv 官方库文件
# set(OpenCV_INCLUDE_DIRS /usr/include)
include_directories(${OpenCV_INCLUDE_DIRS})
# 自定义文件
include_directories(with_opencv/include)
aux_source_directory (with_opencv/src SRC_LIST)
else()
message("Build without OpenCV")
ADD_DEFINITIONS(-DCLOSE_OPENCV)
endif()
endif()
include_directories(include)
aux_source_directory (src SRC_LIST)
add_executable(vedio_opencv main.cpp ${SRC_LIST})
target_link_libraries(vedio_opencv ${OpenCV_LIBS})
当我运行 CMakelist.txt 时,它会显示:
The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.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
Build with OpenCV
-- Found OpenCV: /usr/local/openCV (found version "4.7.0")
-- Opnecv ;ibrary status:
-- > version: 4.7.0
-- libraries: opencv_core;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_photo;opencv_video;opencv_videoio;opencv_bioinspired;opencv_datasets;opencv_freetype;opencv_fuzzy;opencv_hfs;opencv_img_hash;opencv_intensity_transform;opencv_line_descriptor;opencv_phase_unwrapping;opencv_plot;opencv_quality;opencv_reg;opencv_saliency;opencv_surface_matching;opencv_tracking;opencv_xphoto
-- > include: /usr/local/openCV/include/opencv4
-- Configuring done (0.4s)
-- Generating done (0.0s)
-- Build files have been written to: /home/zero/openCV/chip_phytium/build
您遇到的错误表明链接器 (ld) 无法找到您在代码中使用的 CascadeClassifier 方法的定义。当编译过程中未正确链接适当的库(在本例中为 OpenCV)时,通常会发生这种情况。
检查 CMakeLists.txt 是否链接 OpenCV:确保您的 CMakeLists.txt 文件正确找到并链接 OpenCV 库。这是一个基本示例:
project(vedio_opencv)
# Find OpenCV package
find_package(OpenCV REQUIRED)
# Include directories
include_directories(${OpenCV_INCLUDE_DIRS})
# Define the executable
add_executable(vedio_opencv src/cv_zone_Project3.cpp)
# Link OpenCV libraries
target_link_libraries(vedio_opencv ${OpenCV_LIBS})
确保:find_package(OpenCV REQUIRED) 用于查找 OpenCV 库。include_directories(${OpenCV_INCLUDE_DIRS}) 用于包含 OpenCV 头文件。target_link_libraries(vedio_opencv ${OpenCV_LIBS}) 将 OpenCV 库链接到可执行文件。