2016-05-16 109 views
1

我有一個問題,我無法理解。 我想用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中的正確文件夾?我真的不知道是什麼原因造成的! 謝謝!

+1

通過使用命令[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

+0

這很好,謝謝。我仍然很好奇我在哪裏可以找到QT中的環境變量,以及它爲什麼會有所不同。如果您將其張貼爲一個,我會接受您的答案。 – RunOrVeith

回答

0

緩存變量OpenCV_DIR通過搜索命令find_package(在配置模式)進行設置。儘管搜索機制是完全確定性的(在文檔中有描述),但它使用了一些envorinment變量,這些變量在您的shell和QT Creator中可能會有所不同。

只是一個猜測,Qt Creator中可以設置CMAKE_PREFIX_PATH環境變量包含/usr目錄,因此一個/usr/local下才發現/usr下安裝。

對於選擇特定的OpenCV安裝,只需相應地設置OpenCV_DIR緩存變量並重新運行配置過程。