Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Following the book to make the first build work is difficult. #676

Open
ajantti opened this issue Aug 30, 2023 · 6 comments
Open

Following the book to make the first build work is difficult. #676

ajantti opened this issue Aug 30, 2023 · 6 comments
Labels
🪲 bug Something isn't working 🔨 build system Issues related to integrating CXX-Qt into CMake/Cargo 📖 documentation Improvements or additions to documentation

Comments

@ajantti
Copy link

ajantti commented Aug 30, 2023

First thing to note is I'm on Windows.
I tried following the book to build the example. It didn't really work:

  1. First it wouldn't find Qt (and if you follow the book, Qt is not automatically downloaded). The book should at least explain how to make CMake understand where Qt is.
  2. Of course the whole thing with "# Use cxx = "1.0.83" here instead!" is a bit silly. Why does the book give me code that doesn't work and then tells me to fix it?
  3. Then I ran into problems with _ITERATOR_DEBUG_LEVEL not matching, signaling that half is being built in Debug config and half in Release, so I had to build it with --config Release, and I still don't know how to make a debug build.
  4. At this point I figured I'd try to use the example from the repository, so I copied the files from the link given in the book instead. But then I ran into problems with cxx_qt_build::QmlModule not existing. Turns out the book tells me to use the stable versions of the crates, but then links to the development branch of the repo for "check this link if anything doesn't work". (Maybe would be a good idea to make the book link to the latest release tags that is connected to the book.)
    After setting Cargo.toml to get the crates from the repository instead, I got it to build, but running was difficult:
  5. First it had problems with not finding the .dlls. So I added them to PATH.
  6. Then it wouldn't find the platform plugins.
  7. Then it wouldn't find the qml files.
  8. Of course all these would be solved by running windeployqt, but the given CMakeLists.txt doesn't seem to be handling that, and following CMake's instructions didn't work for me either, so I gave up and ran it manually.
  9. Then I even had problems with the qrc. I don't know exactly why the AUTORCC is not working, but shouldn't it be added as a file in the CMakeLists.txt?

Actually, I might as well write it here, (I can make a new issue too), but following the instructions on the Readme.md also had problems:

  1. Running cmake -S . -B build was fine, but I needed to do the same Release thing with cmake --build build --config Release
  2. After that, the exe was in ./build/examples/qml_minimal/Release/example_qml_minimal.exe not ./build/examples/qml_minimal/example_qml_minimal.exe like mentioned
  3. And then I got the same stuff as before about platform plugins.

Honestly at this point I don't know if there's some unmentioned global configuration thing that should be done by some installer, that I haven't done.

Sorry for a bit of a ranty thing. I appreciate the work, and thought you should know what the experience is like.

@LeonMatthesKDAB
Copy link
Collaborator

Hi, thank you for the feedback, it's super valuable, sorry this was such a painful experience for you.

Regarding the issues with the book; we're up for a new release in the near future, so having this feedback is great, we will use this issue as a short list of things to fix when the new release comes up. (Just a few new features left).

For the rest of what you described, that tells me that we do need to test more thoroughly on Windows.
Most of us are on Linux, where installing Qt from the package manager usually sets things up correctly for CMake to find them, so sorry that didn't work for you so easily.
The pointer to windeployqt you mentioned seems to be especially interesting, so we'll check whether that can solve your issues when we get to the next release.

The issues with the readme seem to stem from the _ITERATOR_DEBUG_LEVEL thing you mentioned, so fixing that should also fix your other build. So we'll investigate it. Can you please provide a build log with that error in it.

@ajantti
Copy link
Author

ajantti commented Aug 31, 2023

Sure thing. Here's from fresh git repo (7ee22a9)

First cmake -S . -B build
cmake1.txt

Then cmake --build build
cmake2.txt

So it's somehow trying to link Release and Debug code.

Trying with cmake --build build --config Release succeeds: (although of course this is not a real solution)
cmake3.txt

Trying to run it (note the extra "Release" vs Readme)

PS C:\Nyx\cxx-qt> ./build/examples/qml_minimal/Release/example_qml_minimal
qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Then we have to add the bin to PATH, otherwise it won't find the dlls:
windeployqt.txt

However, still gives weird problems, ("even though it was found"?).

PS C:\Nyx\cxx-qt> ./build/examples/qml_minimal/Release/example_qml_minimal                                              qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: windows.

Trying with copying manually:

PS C:\Nyx\cxx-qt> Remove-Item -Recurse -Force ".\build\examples\qml_minimal\Release\platforms"                          PS PS C:\Nyx\cxx-qt> Copy-Item -Recurse -Path ".\vcpkg\packages\qtbase_x64-windows\Qt6\plugins\platforms" -Destination ".\build\examples\qml_minimal\Release\"

Nope:

PS C:\Nyx\cxx-qt> ./build/examples/qml_minimal/Release/example_qml_minimal
QQmlApplicationEngine failed to load component
qrc:/qt/qml/com/kdab/cxx_qt/demo/qml/main.qml:10:1: module "QtQuick.Window" is not installed
qrc:/qt/qml/com/kdab/cxx_qt/demo/qml/main.qml:9:1: module "QtQuick.Controls" is not installed
qrc:/qt/qml/com/kdab/cxx_qt/demo/qml/main.qml:8:1: module "QtQuick" is not installed
qrc:/qt/qml/com/kdab/cxx_qt/demo/qml/main.qml:10:1: module "QtQuick.Window" is not installed
qrc:/qt/qml/com/kdab/cxx_qt/demo/qml/main.qml:9:1: module "QtQuick.Controls" is not installed
qrc:/qt/qml/com/kdab/cxx_qt/demo/qml/main.qml:8:1: module "QtQuick" is not installed
qrc:/qt/qml/com/kdab/cxx_qt/demo/qml/main.qml:10:1: module "QtQuick.Window" is not installed
qrc:/qt/qml/com/kdab/cxx_qt/demo/qml/main.qml:9:1: module "QtQuick.Controls" is not installed
qrc:/qt/qml/com/kdab/cxx_qt/demo/qml/main.qml:8:1: module "QtQuick" is not installed

At this point I've kind of given up. I think I got it working when I was trying to build the qml_minimal more manually using the system Qt from C:\Qt. But this should give you some idea.

Also for the windeployqt you probably ran/will run into it, but https://www.qt.io/blog/cmake-deployment-api

@ahayzen-kdab
Copy link
Collaborator

I'd also be interested in the output of the following example you posted with QT_DEBUG_PLUGINS=1 set, as it may give a clue to why the plugin is failing to load when it can be found.

PS C:\Nyx\cxx-qt> ./build/examples/qml_minimal/Release/example_qml_minimal
qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: windows.

Note that we assume that qmake (the Qt bin folder) is in the PATH.

And yes windeployqt can be used to create the final binary for distribution, I'm not sure if anyone has tried that yet with CXX-Qt but it shouldn't be any different to normal. However the app should be able to run correctly with the right environment set from the developer binary.

Have you tried if launching from QtCreator helps at all as if a kit is used there it ensures that the environment is correct when running the app as well ?

@ajantti
Copy link
Author

ajantti commented Sep 1, 2023

I didn't try with QtCreator yet. Basically, my first post was me trying to follow the book and use Qt 6.5.1 from system install. The second post was me trying to follow the Readme from the repository.

Although I did now notice that I think I had the system Qt in PATH, but I don't think that matters too much, but it does solve the mystery of the platform plugins. Just in case I removed the PATH, moved the system install to another path so there's no way cmake or anything else finds it and ran everything from scratch:
cmake1.txt
cmake2.txt
run1.txt
cmake3.txt

The reason I realized I was using the wrong windeployqt is that unlike the normal installation, in the vcpkg one windeployqt.exe and all the Qml dlls are in separate folders, so I actually had to do

PS C:\Nyx\cxx-qt> $env:PATH += ";C:\Nyx\cxx-qt\vcpkg\packages\qtbase_x64-windows\bin\"
PS C:\Nyx\cxx-qt> $env:PATH += ";C:\Nyx\cxx-qt\vcpkg\packages\qtbase_x64-windows\tools\Qt6\bin\"
PS C:\Nyx\cxx-qt> $env:PATH += ";C:\Nyx\cxx-qt\vcpkg\packages\qtdeclarative_x64-windows\bin\"

However, after that I got a new error:

PS C:\Nyx\cxx-qt> windeployqt.exe .\build\examples\qml_minimal\Release
Warning: Translations will not be available due to the following error.
Cannot open C:/Nyx/cxx-qt/vcpkg/packages/qtbase_x64-windows/translations/Qt6/catalogs.json
Unable to find dependent libraries of C:\Nyx\cxx-qt\vcpkg\packages\qtbase_x64-windows\bin\Qt6Qml.dll :Cannot open 'C:/Nyx/cxx-qt/vcpkg/packages/qtbase_x64-windows/bin/Qt6Qml.dll': The system cannot find the file specified.

It seems windeployqt expects Qt6Qml to be found in the same bin directory as the others. I think this means windeployqt in the vcpkg might be broken.

(Just for completeness, this is what happens if I run windeployqt from the system install and with QT_DEBUG_PLUGINS=1 I think it fails because the versions don't match run2.txt )

Note that we assume that qmake (the Qt bin folder) is in the PATH.

I do have to say this is an interesting assumption, since it seems like even though the readme mentions Qt as requirement, it also seems to be promising that on Windows Qt is installed from vcpkg. (And if it is installed from vcpkg, why would it be a requirement to be installed normally as well?) So I'm not entirely sure what would be the expected path for the user to take if they want to use the default vcpkg route. Should they also install the exact same matching version of Qt and put it in PATH, or should they add the three vcpkg bin folders to PATH. (Which, if done globally, also kind of defeats the benefit of having a project-local install under cxx-qt.)

It seems the vcpkg route is not that easy, and it can get interference from system installation that people interested in using Qt with Rust probably have, so maybe it shouldn't be the default, at least until it's fool-proof? I would suggest just telling people to install Qt normally (preferably with link), and then have couple of lines about adding things to PATH and maybe setting env CMAKE_PREFIX_PATH to C:\Qt\6.5.1\msvc2019_64\lib\cmake or wherever it is installed. (I'm not sure if that even is the preferred way of telling cmake where to find Qt on Windows, though.)

@OlivierLDff
Copy link
Contributor

This look like a typical windows user story with Qt and CMake.
I went thru similar issue and proposed #498 which might not be the best solution.

  1. Adding an example with aqt install could be easy for windows users.
  2. This can be fixed with setting "CMAKE_MSVC_RUNTIME_LIBRARY": "MultiThreadedDLL" in CMake (and having a recent enough version of cmake that support it.
    5,6,7.
    Add this in you solution env variable:
QML_IMPORT_PATH=$(SolutionDir)/vcpkg_installed/x64-windows/debug/Qt6/qml
QT_PLUGIN_PATH=$(SolutionDir)/vcpkg_installed/x64-windows/debug/Qt6/plugins
PATH=$(SolutionDir)/vcpkg_installed/x64-windows/debug/bin

Checkout the screenshot for reference in my PR.

CMake could take care of those point easily.

  1. CMake can take care of that, I even did an easy to use script: https://github.com/OlivierLDff/QtWindowsCMake

Then in your second row of problems:
2. Could be mitigited by always adding CMAKE_BUILD_TYPE in the path of output exe/lib for single config generator with CMake. I'm doing that for most of my project.

@ahayzen-kdab ahayzen-kdab added 🪲 bug Something isn't working 🔨 build system Issues related to integrating CXX-Qt into CMake/Cargo 📖 documentation Improvements or additions to documentation labels Sep 5, 2023
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Sep 6, 2023
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Sep 6, 2023
As Rust always uses the non-debug Windows runtime for MSVC targets.

Related to KDAB#682
Related to KDAB#676
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Sep 6, 2023
As Rust always uses the non-debug Windows runtime for MSVC targets.

Related to KDAB#682
Related to KDAB#676
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Sep 6, 2023
As Rust always uses the non-debug Windows runtime for MSVC targets.

Related to KDAB#682
Related to KDAB#676
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Sep 6, 2023
As Rust always uses the non-debug Windows runtime for MSVC targets.

Related to KDAB#682
Related to KDAB#676
@ahayzen-kdab
Copy link
Collaborator

So the _ITERATOR_DEBUG_LEVEL is solved using #683.

I've also spent some time fixing my Windows setup and have no confirmed that things at least work and run in a certain scenario.

I have

  • msvc2019
  • Qt 5.15.2 from the Qt online installer
  • CMake from the Qt online installer
  • Git for windows installed into PATH
  • Git set to enable symlinks core.symlinks=true (I also needed to enable Windows 10 developer mode for this to work)
  • I've added C:\Qt\5.15.2\msvc2019_64\bin folder to PATH
  • VCPKG=OFF in CMake config

With this setup QtCreator can configure, build, and run targets from CMake. And from the command line i can run cargo run -p qml-minimal-no-cmake and it shows correctly.

I've not tried vcpkg yet, we use this mainly for CI, and hoped it would help developers on macOS and Windows setup faster - but maybe it confuses matters having it on in CMake by default. I'll try to using the setup with VCPKG=ON next and see what happens for me.

ahayzen-kdab added a commit that referenced this issue Sep 8, 2023
ahayzen-kdab added a commit that referenced this issue Sep 8, 2023
As Rust always uses the non-debug Windows runtime for MSVC targets.

Related to #682
Related to #676
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🪲 bug Something isn't working 🔨 build system Issues related to integrating CXX-Qt into CMake/Cargo 📖 documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

4 participants