如果从 cmd.exe 运行但不在 Qt Creator 中运行,则基于 Qt 的 C++ 项目有效 [英] Qt-Based C++ Project Works If Run From cmd.exe But Not From Within Qt Creator

查看:111
本文介绍了如果从 cmd.exe 运行但不在 Qt Creator 中运行,则基于 Qt 的 C++ 项目有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个非常小的基于 C/C++ Qt 的 Windows 应用程序(仅限 Windows —— 根本不是跨平台),它使用 VISA 库 (visa64.dll) 与一些外部硬件通信.反过来,该库使用其他一些库:

(来自 Dependency Walker 又名depends.exe 的截图)

最初我是在 Visual Studio 中编写的,效果很好.然后我将它移植到 Qt Creator(使用 Qt5,w/MSVC 2015 Visual C++ 工具链)并且我遇到了运行时错误.它知道在哪里可以找到外部头文件,所以我认为我的 INCLUDEPATH 是正确的,而且它构建得很好,所以我认为我的 .pro 文件中的 LIBS 变量是正确的,也就是说它可以找到它需要的 .lib 文件.但是,我从这个外部库 (viOpenDefaultRM) 调用的第一个 API 返回以下错误:VI_ERROR_LIBRARY_NFOUND.无论我进行调试构建还是发布构建,以及是否使用调试器运行它,都会发生这种情况.只要我在 Qt Creator 中运行程序,它就会出现运行时错误.

这是我的 .pro 文件:

TEMPLATE = app配置 += 控制台 c++11配置 -= app_bundle配置 -= qt源 += main.cppINCLUDEPATH += $$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Include'LIBS += -L$$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Lib_x64/msc/' -lvisa64INCLUDEPATH += $$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Include'DEPENDPATH += $$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Include'

/Include 结尾的路径有头文件 (.h)(它是一个 C 库),而以 /msc 结尾的路径有一个 .lib文件..lib 文件不是静态库,它们是一些相应 DLL 的接口文件.这些 DLL 文件位于 C:\System32 中.C:\SysWOW64 中也有 32 位版本.它们也可能存在于其他地方,但如果存在,我不知道.

现在,如果我从 cmd.exe 运行它,它工作正常.也就是说,如果我打开一个 cmd.exe 终端窗口并导航到我的 Qt 项目的构建目录 (c:\blah\blah\blah\obj\debug\) 并从 cmd.exe 运行可执行文件,我没有收到运行时错误.它可以连接到外部硬件,与之交谈,一切都在发生,令人欣喜若狂.

我已经对这个问题进行了大量的搜索和研究,但我有点受诅咒,因为大多数人都有相反的问题,这意味着那个问题(与我的)是 Google/DuckDuckGo/StackOverflow/forum.qt.io/doc.qt.io 搜索中出现的内容.这个问题通常与丢失/错位的 Qt 库有关.

那应该会给你一个东西列表,其中一个叫做Environment.那应该有一个 Change... 按钮,你可以按下:

这应该打开一个包含所有环境内容的新窗口:

(截图来自之后我解决了问题)

这是我发现 PATH 以及一些值得了解的库和包含路径的地方.所以现在我知道我的 Qt Creator PATH 是什么,我打开 cmd.exe 并输入命令 echo %PATH% 来找出什么我的系统 PATH 是.我抓住了与我正在使用的这些 VISA 驱动程序有关的所有内容(基本上是路径中带有 VISA 和/或 IVI Foundation 的所有内容)并将它们粘贴到我的 PATH 在 Qt Creator 中.这是我粘贴在那里以使其工作的内容列表:

C:\WINDOWS\system32;C:\Program Files\IVI Foundation\VISA\Win64\ktvisa;C:\Program Files\IVI Foundation\VISA\Win64\bin;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\bin;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;C:\Program Files\IVI Foundation\VISA\Win64\Bin\;C:\ProgramFiles (x86)\IVI Foundation\VISA\winnt\agvisa;C:\Program Files\Keysight\IO Libraries Suite\bin;C:\Program Files (x86)\Keysight\IO Libraries Suite\bin;C:\Program Files(x86)\IVI Foundation\VISA\WinNT\ktvisa;C:\Program Files (x86)\IVI Foundation\IVI\bin;C:\Program Files\IVI Foundation\IVI\bin;

我添加了c:\system32,因为我知道visa64.dll 就在那里,它至少是我知道我需要的一个顶级DLL.奇怪的是,当我添加 just c:\system32 而没有所有 VISAIVI Foundation 的东西时,那没有用.所以,我不知道我添加到我的 Qt Creator 路径中的所有是否是必要的,因为我刚刚想到了这个解决方案,但是一旦我缩减了列表以找出我实际上所做的一切需要我会在这里添加该信息.以防万一其他人遇到这个问题或好奇.为了完整起见,我想.好的,谢谢大家;)

I am writing a very minimal C/C++ Qt-based application for Windows (only Windows -- not cross platform at all) that uses a VISA library (visa64.dll) to talk to some external hardware. That library, in turn, uses some other libraries:

(screenshot from Dependency Walker a.k.a. depends.exe)

Originally I wrote it in Visual Studio and it worked great. Then I ported it to Qt Creator (using Qt5, w/ MSVC 2015 Visual C++ toolchain) and I got runtime errors. It knows where to find the external header files, so I think my INCLUDEPATH is right, and it builds fine so I think the LIBS variables in my .pro file are right, which is to say it can find the .lib files it needs. However, the first API I call from this external library (viOpenDefaultRM) returns the following error: VI_ERROR_LIBRARY_NFOUND. This happens whether I make a debug build or a release build, and whether or not I am running it with a debugger. As long as I run the program from within Qt Creator, it gets runtime errors.

Here is my .pro file:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

INCLUDEPATH += $$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Include'

LIBS += -L$$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Lib_x64/msc/' -lvisa64

INCLUDEPATH += $$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Include'
DEPENDPATH += $$PWD/'../../../../Program Files/IVI Foundation/VISA/Win64/Include'

The paths that end with /Include have header (.h) files (it's a C library), and the path that ends with /msc has a .lib file. The .lib files are not static libraries, they are the interface files for some corresponding DLLs. Those DLL files are in C:\System32. There are also 32-bit versions in C:\SysWOW64. They may also exist elsewhere but if they do I am not aware of it.

Now, if I run it from cmd.exe it works fine. That is, if I open a cmd.exe terminal window and navigate to my Qt project's build directory (c:\blah\blah\blah\obj\debug\) and run the executable from cmd.exe, I get no runtime errors. It can connect to the external hardware, talk to it, all good things are happening, much rejoicing.

I've done a decent amount of searching and researching about this problem, and I am somewhat cursed by the fact that most people have the opposite problem, which means that problem (the opposite one of mine) is what turns up in Google/DuckDuckGo/StackOverflow/forum.qt.io/doc.qt.io searches. That problem usually has to do with missing/misplaced Qt libraries. Here is an example. The answer to this question usually ends up with a link to a page on how to deploy Qt projects for Windows, e.g. this article.

Also I've read this article from Qt on how to add libraries to your project, and it didn't help me out, but I could be missing something and/or doing it wrong.

This might be something really dumb I'm missing and frankly I hope it is. Thanks*10^6.

解决方案

TL;DR: The kit I was using to compile in Qt Creator had a different PATH set than my system PATH. To fix this, I did echo %PATH in cmd.exe and copied all the stuff that had to do with the drivers I'm trying to use into the PATH for the kit I'm using in Qt Creator. More details below.

I got this to work this morning. As suggested by @adrien-lerevat, when run from within Qt Creator, my executable couldn't find some DLLs it needed. The long and short of it is that I was defining a PATH in my kit (a "kit" in Qt is basically a compiler, a debugger, and some environment variables) that was different from, and not a superset of, my normal system path. I had inherited this kit for other purposes, you see, from other projects, and I didn't realize a PATH could be set in it, or that I was setting one. So to find the PATH I was setting for Qt Creator, I went to the Tools dropdown and selected Options..., then Build & Run, then Kits. Then click on the kit you are using to edit it. As such:

That should give you a list of stuff, one thing of which is called Environment. That should have a Change... button you can press:

which should open a new window with all your environment stuff:

(screenshot is from after I fixed the problem)

This is where I found PATH, as well as some library and include paths that were worth knowing about. So now that I knew what my Qt Creator PATH was, I opened cmd.exe and typed the command echo %PATH% to find out what my system PATH was. I grabbed everything that had to do with these VISA drivers I'm using (basically anything with VISA and/or IVI Foundation in the path) and pasted them into my PATH in Qt Creator. This was the list of stuff I pasted in there to make it work:

C:\WINDOWS\system32;C:\Program Files\IVI Foundation\VISA\Win64\ktvisa;C:\Program Files\IVI Foundation\VISA\Win64\bin;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\bin;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;C:\Program Files\IVI Foundation\VISA\Win64\Bin\;C:\Program Files (x86)\IVI Foundation\VISA\winnt\agvisa;C:\Program Files\Keysight\IO Libraries Suite\bin;C:\Program Files (x86)\Keysight\IO Libraries Suite\bin;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\ktvisa;C:\Program Files (x86)\IVI Foundation\IVI\bin;C:\Program Files\IVI Foundation\IVI\bin;

I added c:\system32 because I know that's where visa64.dll is, which is at least one top-level DLL I know I need. Oddly enough, though, when I added just c:\system32 without all the VISA and IVI Foundation stuff, that didn't work. So, I don't know if everything I added to my Qt Creator path was necessary, as I've just come upon this solution, but once I pare down the list to find out what all I actually needed I will add that information here. Just in case anyone else ever comes across this problem or is curious. And for the sake of completeness I suppose. Okay thanks everyone ;)

这篇关于如果从 cmd.exe 运行但不在 Qt Creator 中运行,则基于 Qt 的 C++ 项目有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆