为什么CLion可以正确构建和链接Qt,但不能运行我的可执行文件? [英] Why can CLion correctly build and link Qt, but not run my executable?
问题描述
我正在尝试在Windows上的Clion中编译和调试Qt代码.CMake和构建过程均未返回任何错误.
I am trying to compile ánd run/debug Qt code in Clion on Windows. Both CMake and the building process return no errors.
发生以下情况:
-
当我尝试运行时,我得到
进程,退出代码为-1073741511(0xC0000139)
当我尝试调试时,我得到进程,退出代码为1
When I try to debug I get Process finished with exit code 1
当我尝试直接通过Windows CMD运行可执行文件时,可执行文件会按预期运行.
When I try to run the executable via Windows CMD directly, the executable runs as intended.
当我将所有dll文件从 ../Qt/5.12.6/mingw73_64/bin
复制到项目的 cmake-build-debug
文件夹中时,可执行文件在CLion中按预期方式运行和调试.
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.
我的设置
- Windows 10
- Qt 5.12.6(mingw73_64构建)
- CLion 2019.2.5
- MinGW(x86_64-8.1.0-win32-seh-rt_v6_rev0)
- CMake(捆绑销售,3.15.3)
我相信StackOverflow上有许多涉及同一问题的相关主题.但是没有人能够对我认为是路径/环境的问题提供明确的答案.许多建议可以归结为将Qt添加到您的path/PATH/Path环境变量,然后重新启动,重新启动,重新启动!",和/或大部分与Linux安装有关.因此,我希望这对于在此情况下遇到相同错误代码的人们来说是一个更完整的问答,因为它很可能与同一问题有关.
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.
由于事情在CLion外部工作(如(3)所示)并且在我复制DLL(4)时在CLion内部工作,因此我认为由于CLion相关的环境问题,我正在处理一个动态链接问题.将Qt bin文件夹( C:\ Qt \ 5.12.6 \ mingw73_64 \ bin
)添加到我的 System Environment Variables
中,这样我可以直接运行exe文件来自CMD.请注意,我将Qt bin文件夹路径添加到 Path
变量.
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.
鉴于有些人在网上提到由于CLion以特定系统用户身份运行,因此用户变量可能是一个问题,我还将该路径添加为 User Environment Variable
(用户环境变量),再次添加了路径
.但是a.
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.
此外,我尝试通过 Settings->将其作为环境变量直接添加到CLion中.外观行为->路径变量
.在这里,我尝试将Qt bin文件夹分别映射到 Path
, PATH
和 QT_DIR
.即使我尝试了多次重新启动,仍然没有成功.在更改路径等之间多次尝试Windows重新启动和真正关闭.
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.
如何解决我描述的问题,所以我可以在CLion中运行和调试Qt构建,而不必将与Qt相关的DLL复制到可执行文件所在的 cmake-build-debug
中.
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.
在设置内->构建,执行,部署->CMake
我已将 CMake选项:
设置为 -DCMAKE_PREFIX_PATH = C:\\ Qt \\ 5.12.6 \\ mingw73_64 \\ lib \\ cmake
>
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 ();
}
推荐答案
同时,这已为我解决.
解决方案如下:
- 转到
编辑配置
- 将Qt bin文件夹添加到
工作目录
- 点击
OK
和/或APPLY
- Go to
edit configurations
- Add the Qt bin folder to
Working directory
- Hit
OK
and/orAPPLY
现在,我可以直接在CLion内部运行和构建Qt应用程序了.
Now I am able to run and build my Qt applications from within CLion directly.
这篇关于为什么CLion可以正确构建和链接Qt,但不能运行我的可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!