Why can CLion correctly build and link Qt, but not run my executable?
Asked Answered
S

1

8

Summary

I am trying to compile ánd run/debug Qt code in Clion on Windows. Both CMake and the building process return no errors.

The following scenarios occur:

  1. When I try to run I get Process finished with exit code -1073741511 (0xC0000139)

  2. When I try to debug I get Process finished with exit code 1

  3. When I try to run the executable via Windows CMD directly, the executable runs as intended.

  4. When I copy all dll files from ../Qt/5.12.6/mingw73_64/bin to the project's cmake-build-debug folder, the executable runs and debugs within CLion as expected.

My setup

  • Windows 10
  • Qt 5.12.6 (mingw73_64 build)
  • CLion 2019.2.5
  • MinGW (x86_64-8.1.0-win32-seh-rt_v6_rev0)
  • CMake (bundled, 3.15.3)

Other findings

I believe there are many related topics on StackOverflow that deal with the same issue. But none manage to provide a definitive answer to what I believe to be a Path/Environment issue. Many suggestions boil down to "Add Qt to your path/PATH/Path environment variable and reboot reboot reboot!", and/or mostly pertain to linux installs. Hence I hope this becomes a more complete question and answer for people running into the same error code within this context, as it is likely related to this same issue.

As things work outside of CLion (as shown by (3)) and work inside of CLion when I copy DLLs (4), I believe I am dealing a dynamic linking issue as a result of CLion related environment issues. Adding the Qt bin folder, which is C:\Qt\5.12.6\mingw73_64\bin, to my System Environment Variables made it so I could run the exe file directly from CMD. Note that I added the Qt bin folder path to the Path variable.

Given that some mentioned online that it is possibly an issue with the user variables due to CLion running as a certain system user, I also added said path as a User Environment Variable, again Path. But alas.

Additionally, I tried adding it as an environment variable directly in CLion via Settings -> Appearance & Behavior -> Path Variables. Here I tried mapping the Qt bin folder to Path, PATH, and QT_DIR respectively. Still no success, even though I tried many reboots. Both Windows restarts and real shutdowns were attempted many times in between changing paths etc.

Main question:

How can I resolve the issue I described, so I can run and debug my Qt builds in CLion without having to copy Qt related DLLs to my cmake-build-debug where the executable is located.

CMakeLists.txt

Within Settings -> Build, Execution, Deployment -> CMake I have set CMake options: to -DCMAKE_PREFIX_PATH=C:\\Qt\\5.12.6\\mingw73_64\\lib\\cmake

cmake_minimum_required(VERSION 3.8)
project(HelloWorld)

set(CMAKE_CXX_STANDARD 14)

# Include a library search using find_package()
# via REQUIRED, specify that libraries are required
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)

set(SOURCE_FILES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})

# specify which libraries to connect
target_link_libraries(${PROJECT_NAME} Qt5::Core)
target_link_libraries(${PROJECT_NAME} Qt5::Gui)
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)

main.cpp

#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QLabel>

int main (int argc, char * argv []) {

    QApplication app (argc, argv);

    QWidget widget;
    widget.resize (640, 480);
    widget.setWindowTitle ("Hello, world !!!");

    QGridLayout * gridLayout = new QGridLayout (& widget);

    QLabel * label = new QLabel ("Hello, world !!!");
    label-> setAlignment (Qt :: AlignVCenter | Qt :: AlignHCenter);
    gridLayout-> addWidget (label);

    widget.show ();

    return app.exec ();
}
Scabby answered 5/12, 2019 at 1:44 Comment(0)
S
15

This has been resolved for me in the meanwhile.

Solution was as follows:

  1. Go to edit configurations
  2. Add the Qt bin folder to Working directory
  3. Hit OK and/or APPLY

Now I am able to run and build my Qt applications from within CLion directly.

Edit configurations option Run/Debug Configurations window

Scabby answered 21/12, 2019 at 21:42 Comment(3)
I'll get you your favourite beverage for that, this took me 24h to finally find this solution.Madigan
Now that I know about this setting it makes perfect sense. Apparently Qt Creator sets PATH and your comment is what was missing from my project in CLion. +1 thanksAnzio
You can also set Environment variables to PATH=$PATH\;C:\Qt\5.12.6\mingw73_64\bin in this dialogFouquiertinville

© 2022 - 2024 — McMap. All rights reserved.