基于CMakeLists编译GStreamer-OpenCV插件时遇未定义引用错误求助
解决GStreamer OpenCV插件编译未定义引用问题
问题背景
从GStreamer的gst-plugins-bad中复制了gstcvequalizehist相关文件,计划修改其中的OpenCV帧处理逻辑,但编译时出现未定义引用错误,涉及gst_opencv_video_filter_get_type和gst_opencv_video_filter_set_in_place函数。
目录结构
└── gstreamer-filter ├── build ├── CMakeLists.txt ├── gstcvequalizehist.cpp ├── include │ ├── gstcvequalizehist.h │ └── gstopencvvideofilter.h └── main.cpp
当前CMakeLists.txt内容
cmake_minimum_required(VERSION 3.10) project(VideoFilter) find_package(OpenCV REQUIRED) find_package(PkgConfig REQUIRED) pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0) pkg_check_modules(GST_APP REQUIRED gstreamer-app-1.0) include_directories(include) include_directories(${OpenCV_INCLUDE_DIRS}) include_directories(${GSTREAMER_INCLUDE_DIRS}) include_directories(${GST_APP_INCLUDE_DIRS}) include_directories(${GST_OPENCV_INCLUDE_DIRS}) link_directories(${GST_OPENCV_LIBRARY_DIRS}) # Link directories link_directories(${OpenCV_LIBRARY_DIRS}) link_directories(${GSTREAMER_LIBRARY_DIRS}) link_directories(${GST_APP_LIBRARY_DIRS}) add_executable(VideoFilter main.cpp gstcvequalizehist.cpp) target_link_libraries(VideoFilter ${GSTREAMER_LIBRARIES} ${GST_APP_LIBRARIES} ${OpenCV_LIBRARIES} ${GST_OPENCV_LIBRARIES})
编译错误输出
-- The C compiler identification is GNU 11.4.0 -- The CXX compiler identification is GNU 11.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 CMake Warning (dev) at /usr/local/lib/cmake/opencv4/OpenCVConfig.cmake:86 (find_package): Policy CMP0146 is not set: The FindCUDA module is removed. Run "cmake --help-policy CMP0146" for policy details. Use the cmake_policy command to set the policy and suppress this warning. Call Stack (most recent call first): /usr/local/lib/cmake/opencv4/OpenCVConfig.cmake:108 (find_host_package) CMakeLists.txt:7 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. -- Performing Test CMAKE_HAVE_LIBC_PTHREAD -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success -- Found Threads: TRUE -- Found CUDA: /usr/local/cuda (found suitable exact version "12.1") -- Found OpenCV: /usr/local (found version "4.7.0") -- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2") -- Checking for module 'gstreamer-1.0' -- Found gstreamer-1.0, version 1.20.3 -- Checking for module 'gstreamer-app-1.0' -- Found gstreamer-app-1.0, version 1.20.1 -- Configuring done (1.0s) -- Generating done (0.0s) -- Build files have been written to: gstreamer-filter/build [ 33%] Building CXX object CMakeFiles/VideoFilter.dir/main.cpp.o [ 66%] Building CXX object CMakeFiles/VideoFilter.dir/gstcvequalizehist.cpp.o [100%] Linking CXX executable VideoFilter /usr/bin/ld: CMakeFiles/VideoFilter.dir/gstcvequalizehist.cpp.o: in function `gst_cv_equalize_hist_get_type_once()': gstcvequalizehist.cpp:(.text+0x10a): undefined reference to `gst_opencv_video_filter_get_type' /usr/bin/ld: CMakeFiles/VideoFilter.dir/gstcvequalizehist.cpp.o: in function `gst_cv_equalize_hist_init(_GstCvEqualizeHist*)': gstcvequalizehist.cpp:(.text+0x26f): undefined reference to `gst_opencv_video_filter_set_in_place' collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/VideoFilter.dir/build.make:182: VideoFilter] Error 1 make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/VideoFilter.dir/all] Error 2 make: *** [Makefile:91: all] Error 2
解决方案
1. 安装依赖包
未定义的函数来自GStreamer的OpenCV扩展库,先安装对应的开发包:
sudo apt-get install libgstreamer-opencv1.0-dev
2. 修正CMakeLists.txt
原配置中GST_OPENCV变量未被正确初始化,需要通过pkg-config引入gstreamer-opencv模块,修改后的配置如下:
cmake_minimum_required(VERSION 3.10) project(VideoFilter) find_package(OpenCV REQUIRED) find_package(PkgConfig REQUIRED) pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0) pkg_check_modules(GST_APP REQUIRED gstreamer-app-1.0) # 添加gstreamer-opencv模块 pkg_check_modules(GST_OPENCV REQUIRED gstreamer-opencv-1.0) include_directories(include) include_directories(${OpenCV_INCLUDE_DIRS}) include_directories(${GSTREAMER_INCLUDE_DIRS}) include_directories(${GST_APP_INCLUDE_DIRS}) include_directories(${GST_OPENCV_INCLUDE_DIRS}) link_directories(${OpenCV_LIBRARY_DIRS}) link_directories(${GSTREAMER_LIBRARY_DIRS}) link_directories(${GST_APP_LIBRARY_DIRS}) link_directories(${GST_OPENCV_LIBRARY_DIRS}) add_executable(VideoFilter main.cpp gstcvequalizehist.cpp) target_link_libraries(VideoFilter ${GSTREAMER_LIBRARIES} ${GST_APP_LIBRARIES} ${OpenCV_LIBRARIES} ${GST_OPENCV_LIBRARIES} )
3. 重新编译
清理旧编译文件后重新执行编译流程:
cd build rm -rf * cmake .. make
说明
gst_opencv_video_filter是GStreamer OpenCV插件的基础类,提供了OpenCV帧处理的通用逻辑,必须链接对应的库才能解决未定义引用问题。
内容的提问来源于stack exchange,提问作者Ja_cpp
相关产品推荐
相关产品推荐

