简体   繁体   English

使用 cmake 从源代码构建应用程序和 Qt

[英]Build application and Qt from source using cmake

I'm trying to set up building an application that uses qt5.6 in a way that both qt and the application are build from source (using the ninja generator, with visual studio compiler on windows and clang on mac).我试图建立一个使用 qt5.6 的应用程序,qt 和应用程序都是从源代码构建的(使用 ninja 生成器,在 windows 上使用 Visual Studio 编译器,在 mac 上使用 clang)。

I'm stuck at find_package(Qt5Core ..) : when Qt is not build yet, it will not be found.我被困在 find_package(Qt5Core ..) :当 Qt 还没有构建时,它不会被找到。 And because it's not found, the generate cmake file is not complete.并且因为没有找到,所以生成的cmake文件不完整。

I think I need a setup where it generates a ninja files that, when build, builds Qt and then regenerates the ninja file (and at this point it would find qt) before continuing the build.我想我需要一个设置,它会生成一个 ninja 文件,该文件在构建时构建 Qt,然后在继续构建之前重新生成 ninja 文件(此时它会找到 qt)。

Or any other way in which I can build Qt+application from source, so that if I change something in Qt, it is automatically rebuild.或者我可以从源代码构建 Qt + 应用程序的任何其他方式,以便如果我在 Qt 中更改某些内容,它会自动重建。

How should I set up my cmake file(s) to do that?我应该如何设置我的 cmake 文件来做到这一点?

You could use CMake's ExternalProject command to invoke CMake from within CMake.您可以使用 CMake 的ExternalProject命令从 CMake 中调用 CMake。 You can specify dependencies there, so that your application will only be built after Qt has been built.您可以在那里指定依赖项,以便您的应用程序仅在构建 Qt 之后构建。

I happend to have a small example here that uses ExternalProject_Add to build a library followed by an application.我碰巧在这里有一个小例子,它使用ExternalProject_Add构建一个库,然后是一个应用程序。 In that example, CMake for the library and the application is invokved at make time.在该示例中,库和应用程序的 CMake 在make时被调用。

cmake_minimum_required(VERSION 3.0)

include(ExternalProject)

ExternalProject_Add(cmake_lib
    URL ../cmake_lib
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
)

ExternalProject_Add(cmake_app
    DEPENDS cmake_lib
    URL ../cmake_app
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
)

The problem with Qt 5 is that it doesn't use cmake to get built, and thus your ninja build process won't know anything about the internals of Qt unless you tell it everything. Qt 5 的问题在于它不使用 cmake 来构建,因此除非您将所有内容都告诉它,否则您的 ninja 构建过程不会了解 Qt 的内部结构。 Eg if you change any dependencies of Qt, or if Qt gets partially built, you'd have to add a lot of knowledge to your project's build system to determine whether the Qt build needs to be invoked again.例如,如果您更改 Qt 的任何依赖项,或者如果 Qt 被部分构建,您必须向项目的构建系统添加大量知识,以确定是否需要再次调用 Qt 构建。 This can be certainly made to work in a "fire and forget" style of build, where the build always starts from scratch, like in a CI system.这当然可以在“即发即忘”的构建风格中工作,其中构建总是从头开始,就像在 CI 系统中一样。 One quickly runs into serious trouble if the intent is to change Qt itself and re-run the build.如果目的是更改 Qt 本身并重新运行构建,那么很快就会遇到严重的麻烦。 Even Qt's own build system has serious trouble with full dependency tracking due to fundamental architectural decisions in qmake.由于 qmake 中的基本架构决策,即使 Qt 自己的构建系统在完全依赖跟踪方面也存在严重问题。

Qmake-based developer re-builds of Qt suck - and they do to such an extent that I didn't bother contributing to the project since it felt like a penance, where the simplest of changes to one source file would take a minute or more to rebuild on, and any changes to qmake project files would sometimes ballon into multi-minute affairs.基于 Qmake 的开发人员重新构建 Qt 很糟糕 - 他们这样做的程度以至于我没有为该项目做出贡献,因为它感觉像是一种忏悔,对一个源文件的最简单的更改需要一分钟或更长时间重建,对 qmake 项目文件的任何更改有时都会变成多分钟的事务。

The only solution that I have found that actually works and doesn't make you hate life, the universe and everything, was to reimplement Qt's build and configuration system using cmake, so that qmake becomes unnecessary.我发现的唯一真正有效并且不会让你讨厌生活、宇宙和一切的解决方案是使用 cmake 重新实现 Qt 的构建配置系统,这样 qmake 就变得不必要了。 As a result, a statically linked unity debug build of qtbase takes a couple of minutes with all features enabled - it's pretty zippy and is much faster than the fastest option provided by Qt 5 on Windows for MSVC builds: qmake+jom.因此,在启用所有功能的情况下,qtbase 的静态链接统一调试构建需要几分钟的时间 - 它非常快速,并且比 Windows 上的 Qt 5 为 MSVC 构建提供的最快选项:qmake+jom 快得多。

The Qt project endeavored to do this during Qt 6 development, and I believe that they were successful :) Qt 项目在 Qt 6 开发期间努力做到这一点,我相信他们是成功的 :)

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

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