我有一个问题,我无法理解。 我想用openCV 3.1编写一个项目。我想在QT Creator环境中编写代码,因为我已经习惯了。我现在不需要任何QT特定功能,我正在使用Ubuntu 14.04。 问题是,无论何时我尝试在QT Creator内部构建,它都不会生成以下错误,但是如果从命令行构建,它将起作用。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靛蓝项目。但是,如果我执行完全相同的cmake的文件,并从命令行构建,它完美的作品:
[email protected]:/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
[email protected]:/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,它会找到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建立在检查窗口,我看到OpenCV_DIR变量被设置为2件不同的事情:在命令行:
OpenCV_DIR /usr/local/share/OpenCV
在QT创建者:
OpenCV_DIR /ust/share/OpenCV
如何将其设置为自动选择QT Creator中的正确文件夹?我真的不知道是什么原因造成的! 谢谢!
通过使用命令[find_package](https://cmake.org/cmake/help/v3.0/command/find_package.html)(在* config *模式下)进行搜索来设置缓存变量'OpenCV_DIR'。虽然搜索机制是完全确定性的(它在文档中有描述),但它使用了一些在你的shell和QT Creator下可能不同的环境变量。只是猜测,QT Creator可能会将'CMAKE_PREFIX_PATH' * environment *变量设置为包含'/ usr'。为了选择特定的OpenCV安装,只需相应地设置“OpenCV_DIR”缓存变量并重新运行配置过程。 – Tsyvarev
这很好,谢谢。我仍然很好奇我在哪里可以找到QT中的环境变量,以及它为什么会有所不同。如果您将其张贴为一个,我会接受您的答案。 – RunOrVeith