QT Creator 链接与命令行不同的库

QT Creator links different library than command line

我有一个我无法理解的问题。 我正在尝试使用 openCV 3.1 编写项目代码。我想在 QT Creator 环境中编写代码,因为我已经习惯了。目前我不一定需要任何 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 个不同的包含路径,具体取决于我从哪里开始 cmake。

我的 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 构建检查 window,我看到 OpenCV_DIR 变量设置为 2 个不同的东西:在命令行上:

 OpenCV_DIR                       /usr/local/share/OpenCV    

在 QT Creator 中:

OpenCV_DIR                           /ust/share/OpenCV

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

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

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

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