【发布时间】:2016-09-12 00:54:12
【问题描述】:
我有一个我无法理解的问题。 我正在尝试使用 openCV 3.1 编写一个项目。我想在 QT Creator Environment 中编写代码,因为我已经习惯了。目前我不一定需要任何 QT 特定功能,我正在使用 Ubuntu 14.04。 问题是,每当我尝试在 QT Creator 中构建时,它不会出现以下错误,但如果我从命令行构建,它就可以工作。
10:57:36: Starting: "/usr/bin/cmake" --build . --target all
Linking CXX executable arucoTest
CMakeFiles/arucoTest.dir/PoseEstimator.cpp.o: In function `PoseEstimator::generateMarkers(std::string, int)':
PoseEstimator.cpp:(.text+0x56): undefined reference to `cv::aruco::getPredefinedDictionary(cv::aruco::PREDEFINED_DICTIONARY_NAME)'
PoseEstimator.cpp:(.text+0xb7): undefined reference to `cv::aruco::drawMarker(cv::Ptr<cv::aruco::Dictionary>&, int, int, cv::_OutputArray const&, int)'
PoseEstimator.cpp:(.text+0x1a9): undefined reference to `cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
CMakeFiles/arucoTest.dir/PoseEstimator.cpp.o: In function `cv::String::~String()':
PoseEstimator.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
CMakeFiles/arucoTest.dir/PoseEstimator.cpp.o: In function `cv::String::String(std::string const&)':
PoseEstimator.cpp:(.text._ZN2cv6StringC2ERKSs[_ZN2cv6StringC5ERKSs]+0x69): undefined reference to `cv::String::allocate(unsigned long)'
collect2: error: ld returned 1 exit status
make[2]: *** [arucoTest] Error 1
make[1]: *** [CMakeFiles/arucoTest.dir/all] Error 2
make: *** [all] Error 2
10:57:36: The process "/usr/bin/cmake" exited with code 2.
所以这是某种链接器错误。当我检查我的 cmake 文件和输出时,我看到了这个:
Running "/usr/bin/cmake /XYZ/arucoTest '-GCodeBlocks - Unix Makefiles'" in /XYZ/arucoTest/testbuild.
-- OpenCV library status:
-- version: 2.4.8
-- libraries: opencv_videostab;opencv_video;opencv_ts;opencv_superres;opencv_stitching;opencv_photo;opencv_ocl;opencv_objdetect;opencv_ml;opencv_legacy;opencv_imgproc;opencv_highgui;opencv_gpu;opencv_flann;opencv_features2d;opencv_core;opencv_contrib;opencv_calib3d
-- include path: /usr/include/opencv;/usr/include
-- Configuring done
-- Generating done
-- Build files have been written to: /XYZ/arucoTest/testbuild
如您所见,它只找到 openCV 2.4.8,由于旧的 ROS Indigo 项目,我仍然需要它。但是,如果我执行完全相同的 cmake 文件并从命令行构建,它会完美运行:
username@pc:/XYZ/arucoTest/build$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- OpenCV library status:
-- version: 3.1.0
-- libraries: opencv_xphoto;opencv_xobjdetect;opencv_ximgproc;opencv_xfeatures2d;opencv_tracking;opencv_text;opencv_surface_matching;opencv_structured_light;opencv_stereo;opencv_saliency;opencv_rgbd;opencv_reg;opencv_plot;opencv_optflow;opencv_line_descriptor;opencv_fuzzy;opencv_face;opencv_dpm;opencv_dnn;opencv_datasets;opencv_ccalib;opencv_bioinspired;opencv_bgsegm;opencv_aruco;opencv_viz;opencv_videostab;opencv_videoio;opencv_video;opencv_superres;opencv_stitching;opencv_shape;opencv_photo;opencv_objdetect;opencv_ml;opencv_imgproc;opencv_imgcodecs;opencv_highgui;opencv_flann;opencv_features2d;opencv_core;opencv_calib3d
-- include path: /usr/local/include/opencv;/usr/local/include
-- Configuring done
-- Generating done
-- Build files have been written to: /XYZ/arucoTest/build
veith@roethlinLT:/XYZ/arucoTest/build$ make
Scanning dependencies of target arucoTest
[100%] Building CXX object CMakeFiles/arucoTest.dir/PoseEstimator.cpp.o
Linking CXX executable arucoTest
[100%] Built target arucoTest
如您所见,我在 2 个不同的文件夹中构建,因为我发现如果我在同一个文件夹中进行 cmake,它不起作用,即使我删除了该文件夹中的所有内容并从命令行再次 cmake。另请注意,它会根据我启动 cmake 的位置找到 2 个不同的包含路径。
我的 makefile 看起来像这样(不是很疯狂),我在两种情况下都使用这个完全相同的文件。
project(arucoTest)
cmake_minimum_required(VERSION 2.8)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
set(HEADER_LIST PoseEstimator.h)
set(SRC_LIST PoseEstimator.cpp)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
使用 ccmake 和 QT build in 检查窗口,我看到 OpenCV_DIR 变量设置为 2 个不同的东西: 在命令行:
OpenCV_DIR /usr/local/share/OpenCV
在 QT Creator 中:
OpenCV_DIR /ust/share/OpenCV
如何设置它以自动选择 QT Creator 中的正确文件夹?我真的不知道是什么原因造成的! 谢谢!
【问题讨论】:
-
缓存变量
OpenCV_DIR通过搜索命令find_package设置(在config模式下)。虽然搜索机制是完全确定的(在文档中进行了描述),但它使用了一些环境变量,这些变量在您的 shell 和 QT Creator 下可能会有所不同。只是猜测,QT Creator 可能会将CMAKE_PREFIX_PATHenvironment 变量设置为包含/usr。选择特定的 OpenCV 安装,只需相应地设置OpenCV_DIR缓存变量并重新运行配置过程。 -
效果很好,谢谢。我仍然很好奇在哪里可以找到 QT 中的环境变量以及为什么它可能会有所不同。如果您将其作为一个发布,我会接受您的回答。
标签: opencv linker cmake qt-creator include-path