首页 > 解决方案 > 如何避免在 cmake 中自动链接 Qt5 库?

问题描述

我打算编译以下opencv代码:

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char** argv )
{
   printf("hello world \n");
   return 0;
}

我的 CMakeLists.txt 如下:

cmake_minimum_required(VERSION 2.8)
find_package( OpenCV REQUIRED )
project( main )
add_executable(main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )

编译后的结果如下:

[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
/usr/bin/ld: cannot find -lQt5::Core
/usr/bin/ld: cannot find -lQt5::Gui
/usr/bin/ld: cannot find -lQt5::Widgets
/usr/bin/ld: cannot find -lQt5::Test
/usr/bin/ld: cannot find -lQt5::Concurrent
/usr/bin/ld: cannot find -lQt5::OpenGL
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:143: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

我相信 CMake 以自动方式包含 Qt5 库,我想知道如何避免这种情况。

标签: c++cmakelinkerqt5qt4

解决方案


Ideology of CMake is to never add something that is not required. It is possible that Qt libs were added to configuration list of CMake.

They might be featured in standard variable you use. You can print content by

message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}") 

Configuration file looks OpenCVConfig.cmake.in and may include other files which for some reason were changed.


推荐阅读