繁体   English   中英

使用 CMake 检测 Qt5

[英]Detecting Qt5 with CMake

我正在尝试在 Ubuntu 上安装和使用 Qt 5。 为我需要 Qt 5 的项目运行 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
-- 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?

查看包含有关如何使用 CMake 的部分的 Qt 5 文档: http : //doc.qt.io/qt-5/cmake-manual.html

它提供了一个例子:

cmake_minimum_required(VERSION 2.8.11)

project(testproject)

# 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(helloworld WIN32 main.cpp)

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

请注意包含find_package的行,该行与您的行不同。 它还包含建议,如何帮助 CMake 找到您的 Qt 安装:

为了使 find_package 成功,必须在 CMAKE_PREFIX_PATH 下找到 Qt 5,或者必须在 CMake 缓存中将 Qt5_DIR 设置为 Qt5WidgetsConfig.cmake 文件的位置。 使用 CMake 的最简单方法是将 CMAKE_PREFIX_PATH 环境变量设置为 Qt 5 的安装前缀。

进一步阅读: CMake:以正确的方式寻找 Qt5

我加入由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 元素的源文件(更改 #包括从 QtGui 到 QtWidgets 的目录,它不应该由 KDevelop 制作吗?)我将 CMakeLists.txt 转换为显示的内容。 也许不是最佳的,但有效。 (这是我对 Qt 的第一次尝试,我想所提到的困难会让喜欢开箱即用解决方案的人望而却步)

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM