2013-03-26 28 views
27

Tôi đang cố tạo một ứng dụng Qt5 rất cơ bản bằng CMake trên các cửa sổ. Tôi đã sử dụng tài liệu of qt5 to use CMake và tệp main.cpp của tôi chỉ chứa một hàm chính.Cách tìm mô-đun CMake qt5 trên các cửa sổ

My CMakeLists.txt là chính xác:

cmake_minimum_required(VERSION 2.8.9) 

project(testproject) 

giải pháp EDIT

set (CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.0.1\\5.0.1\\msvc2010\\") 

cuối EDIT

# 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 hello.cpp) 

# Use the Widgets module from Qt 5. 
qt5_use_modules(helloworld Widgets) 

Khi ở MSysGit bash tôi nhập `$ cmake - G "Visual Studio 11"

tôi nhận được kết quả này:

$ cmake -G"Visual Studio 11" 
-- The C compiler identification is MSVC 17.0.60204.1 
-- The CXX compiler identification is MSVC 17.0.60204.1 
-- Check for working C compiler using: Visual Studio 11 
-- Check for working C compiler using: Visual Studio 11 -- works 
-- Detecting C compiler ABI info 
-- Detecting C compiler ABI info - done 
-- Check for working CXX compiler using: Visual Studio 11 
-- Check for working CXX compiler using: Visual Studio 11 -- works 
-- Detecting CXX compiler ABI info 
-- Detecting CXX compiler ABI info - done 
CMake Warning at CMakeLists.txt:11 (find_package): 
    By not providing "FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project 
    has asked CMake to find a package configuration file provided by 
    "Qt5Widgets", but CMake did not find one. 

    Could not find a package configuration file provided by "Qt5Widgets" with 
    any of the following names: 

    Qt5WidgetsConfig.cmake 
    qt5widgets-config.cmake 

    Add the installation prefix of "Qt5Widgets" to CMAKE_PREFIX_PATH or set 
    "Qt5Widgets_DIR" to a directory containing one of the above files. If 
    "Qt5Widgets" provides a separate development package or SDK, be sure it has 
    been installed. 


CMake Error at CMakeLists.txt:17 (qt5_use_modules): 
    Unknown CMake command "qt5_use_modules". 


-- Configuring incomplete, errors occurred! 

Bạn có bất cứ ý tưởng?

cảm ơn bạn trước.

Trả lời

29

Việc này sẽ giải quyết được sự cố.

set (CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.0.1\\5.0.1\\msvc2010\\")

Tôi đã chỉnh sửa bài đăng đầu tiên.

+0

tôi đã cùng một vấn đề. Tôi đã sử dụng cmake 2.8.9 và cố gắng làm điều tương tự với CMAKE_MODULE_PATH. Nó đã lờ tôi đi. Sử dụng CMAKE_PREFIX_PATH, nó hoạt động. Có lẽ, một cái gì đó thay đổi trên phiên bản này của cmake ... Cảm ơn! – jorgeas80

+2

Không yêu cầu người dùng của bạn thay đổi CMakeLists như thế. Đặt biến môi trường thay vào đó, như tài liệu Qt nói để làm. – steveire

+0

@steveire Tôi đồng ý nhưng bạn có thể chỉ ra tài liệu QT nơi điều này được giải thích không? – thomas

15

Bạn nên đặt biến môi trường CMAKE_PREFIX_PATH thay thế hoặc sử dụng cmake-gui để đặt đường dẫn đến gói Qt 5.

+1

Tôi cũng khuyên mọi người nên xem câu trả lời của dzada để biết ví dụ về đường dẫn tìm tiền tố Qt cần thiết bởi CMake. –

0

Một cách là mở CMakeLists.txt trong Trình tạo Qt. Qt Creator hỗ trợ CMake nguyên bản và luôn biết Qt ở đâu.

+0

Điều này hoàn toàn không đúng. – steveire

+1

Bạn có thể giải thích tại sao không? Tôi có thể cấu hình thông qua Qt Creator mặc dù Qt thậm chí không nằm trong PATH. CMake GUI thay vì không có đầu mối về Qt. – user2061057

0

Dưới đây là một kỹ thuật tận dụng khả năng của cmake để đọc sổ đăng ký để ép buộc giá trị đăng ký vào vị trí phù hợp của msvc Qt5Config.cmake.

Cố gắng sử dụng phiên bản Qt5 có sẵn cao nhất bằng cách sắp xếp ngược lại trên các tên thư mục "5.x" khác nhau bên trong (ví dụ: C:\Qt\).

Điều này cũng có thể được đặt bên trong một mô-đun, ví dụ: QtLocator.cmake.

SET(QT_MISSING True) 
# msvc only; mingw will need different logic 
IF(MSVC) 
    # look for user-registry pointing to qtcreator 
    GET_FILENAME_COMPONENT(QT_BIN [HKEY_CURRENT_USER\\Software\\Classes\\Applications\\QtProject.QtCreator.cpp\\shell\\Open\\Command] PATH) 

    # get root path so we can search for 5.3, 5.4, 5.5, etc 
    STRING(REPLACE "/Tools" ";" QT_BIN "${QT_BIN}") 
    LIST(GET QT_BIN 0 QT_BIN) 
    FILE(GLOB QT_VERSIONS "${QT_BIN}/5.*") 
    LIST(SORT QT_VERSIONS) 

    # assume the latest version will be last alphabetically 
    LIST(REVERSE QT_VERSIONS) 

    LIST(GET QT_VERSIONS 0 QT_VERSION) 

    # fix any double slashes which seem to be common 
    STRING(REPLACE "//" "/" QT_VERSION "${QT_VERSION}") 

    # do some math trickery to guess folder 
    # - qt uses (e.g.) "msvc2012" 
    # - cmake uses (e.g.) "1800" 
    # - see also https://cmake.org/cmake/help/v3.0/variable/MSVC_VERSION.html 
    MATH(EXPR QT_MSVC "2000 + (${MSVC_VERSION} - 600)/100") 

    # check for 64-bit os 
    # may need to be removed for older compilers as it wasn't always offered 
    IF(CMAKE_SYSTEM_PROCESSOR MATCHES 64) 
     SET(QT_MSVC "${QT_MSVC}_64") 
    ENDIF() 
    SET(QT_PATH "${QT_VERSION}/msvc${QT_MSVC}") 
    SET(QT_MISSING False) 
ENDIF() 

# use Qt_DIR approach so you can find Qt after cmake has been invoked 
IF(NOT QT_MISSING) 
    MESSAGE("-- Qt found: ${QT_PATH}") 
    SET(Qt5_DIR "${QT_PATH}/lib/cmake/Qt5/") 
    SET(Qt5Test_DIR "${QT_PATH}/lib/cmake/Qt5Test") 
ENDIF() 

Và sau đó ..

# finally, use Qt5 + COMPONENTS technique, compatible with Qt_DIR 
FIND_PACKAGE(Qt5 COMPONENTS Core Gui Widgets Xml REQUIRED) 
Các vấn đề liên quan