用CMake检测Qt5

时间:2016-02-02 20:40:29

标签: cmake qt5

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

-- 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?

2 个答案:

答案 0 :(得分:2)

查看Qt 5文档,其中包含有关如何使用CMake的部分: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成功,必须在下面找到Qt 5   必须在CMake中设置CMAKE_PREFIX_PATH或Qt5_DIR   缓存到Qt5WidgetsConfig.cmake文件的位置。最简单的   使用CMake的方法是设置CMAKE_PREFIX_PATH环境变量   到Qt 5的安装前缀。

进一步阅读:CMake: Finding Qt5 the right way

答案 1 :(得分:2)

我按照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的第一次尝试,我猜这些提到的困难可以阻止喜欢开箱即用解决方案的人们)

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)