用CMake检测Qt5 [英] Detecting Qt5 with CMake

查看:201
本文介绍了用CMake检测Qt5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Ubuntu上安装并使用Qt 5。为需要Qt 5的项目运行CMake会导致:

I am trying to install and use Qt 5 on Ubuntu. Running CMake for my project which requires Qt 5 leads to:

-- 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at /usr/local/share/cmake-3.2/Modules/FindQt4.cmake:626 (message):
  /usr/bin/qmake reported QT_INSTALL_LIBS as "/usr/lib/x86_64-linux-gnu" but
  QtCore could not be found there.  Qt is NOT installed correctly for the
  target build environment.

我尝试了来自

https://askubuntu.com/questions/508503/whats-the- development-package-for-qt5-in-14-04

https://www.kdab.com/using-cmake-with-qt-5/

如何在CMake中查找和使用Qt 5?

How to find and use Qt 5 with CMake?

推荐答案

我按照usr1234567的建议添加了正确的 CMAKE_PREFIX_PATH
(安装程序不应该这样做吗?)

之后添加了包含目录 https://www.kdab.com/using-cmake-with-qt-5/
(不应将其包含在手册 http://doc.qt.io/qt-5/cmake-manual.html ,以及KDevelop生成的CMakeLists.txt吗?),进一步重新生成GUI元素
的源文件(将#include目录从QtGui更改为QtWidgets之后,是否应该由KDevelop制作?)
我将CMakeLists.txt转换为所示内容。也许不是最优的,但是可行。
(这是我对Qt的首次尝试,我想提到的困难会使那些喜欢即用型解决方案的人望而却步)

I added the correct "CMAKE_PREFIX_PATH" as advised by usr1234567 (should it not be made by the installer?) added the include directories following https://www.kdab.com/using-cmake-with-qt-5/ (should it not be included in the manual http://doc.qt.io/qt-5/cmake-manual.html, and the KDevelop-generated CMakeLists.txt ?), furthermore the source file generating the GUI elements (after changing the #include directories from QtGui to QtWidgets, should it not be made by KDevelop?) I transformed the CMakeLists.txt to what shown. Maybe not optimal, but works. (it is my very first attempt with Qt, and I guess the mentioned difficulties can discourage people who prefer out-of-box solutions)

cmake_minimum_required(VERSION 2.8.11)

project(MyFirst)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5Widgets)

# Tell CMake to create the helloworld executable
add_executable(MyFirst main.cpp MyFirst.cpp)

# The Qt5Widgets_INCLUDES also includes the include directories for
  # dependencies QtCore and QtGui
  include_directories(${Qt5Widgets_INCLUDES})

  # We need add -DQT_WIDGETS_LIB when using QtWidgets in Qt 5.
  add_definitions(${Qt5Widgets_DEFINITIONS})

  # Executables fail to build with Qt 5 in the default configuration
  # without -fPIE. We add that here.
  set(CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")

# Use the Widgets module from Qt 5.
target_link_libraries(MyFirst Qt5::Widgets)

这篇关于用CMake检测Qt5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆