QT Creator链接不同的库而不是命令行

时间:2016-05-16 09:16:42

标签: opencv linker cmake qt-creator include-path

我遇到了一个我无法理解的问题。 我正在尝试使用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找到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 Creator中:

OpenCV_DIR                           /ust/share/OpenCV

如何将其设置为自动选择QT Creator中的正确文件夹?我真的不知道是什么原因引起的! 谢谢!

1 个答案:

答案 0 :(得分:0)

通过使用命令find_package(在 config 模式下)进行搜索来设置缓存变量OpenCV_DIR。虽然搜索机制是完全确定的(在文档中有描述),但它使用了一些 envorinment变量,它们可能在您的shell和QT Creator中有所不同。

猜测一下,QT Creator可能会将CMAKE_PREFIX_PATH环境变量设置为包含/usr目录,因此它会在/usr下的/usr/local下找到一个安装。

要选择特定的OpenCV安装,只需相应地设置OpenCV_DIR缓存变量,然后重新运行配置过程。