我正在尝试在 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.
Run Code Online (Sandbox Code Playgroud)
我尝试了来自的提示
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?
我按照 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)
Run Code Online (Sandbox Code Playgroud)