简体   繁体   English

基于 Qt 的 C++ 项目如果从 cmd.exe 运行但不能从 Qt Creator 中运行,则可以工作

[英]Qt-Based C++ Project Works If Run From cmd.exe But Not From Within 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.我正在为 Windows(仅 Windows - 根本不是跨平台)编写一个非常小的基于 C/C++ Qt 的应用程序,它使用 VISA 库(visa64.dll)与一些外部硬件通信。 That library, in turn, uses some other libraries:反过来,该库使用其他一些库: Dependency Walker/depends.exe 的屏幕截图

(screenshot from Dependency Walker aka depends.exe) (来自 Dependency Walker 又名depends.exe 的屏幕截图)

Originally I wrote it in Visual Studio and it worked great.最初我是在 Visual Studio 中编写的,效果很好。 Then I ported it to Qt Creator (using Qt5, w/ MSVC 2015 Visual C++ toolchain) and I got runtime errors.然后我将它移植到 Qt Creator(使用 Qt5,w/MSVC 2015 Visual C++ 工具链),我得到了运行时错误。 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.它知道在哪里可以找到外部 header 文件,所以我认为我的 INCLUDEPATH 是正确的,并且它构建得很好,所以我认为 my.pro 文件中的 LIBS 变量是正确的,也就是说它可以找到它需要的 .lib 文件。 However, the first API I call from this external library ( viOpenDefaultRM ) returns the following error: VI_ERROR_LIBRARY_NFOUND .但是,我从这个外部库 ( viOpenDefaultRM ) 调用的第一个 API 返回以下错误: 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.只要我从 Qt Creator 中运行程序,它就会出现运行时错误。

Here is my.pro file:这是 my.pro 文件:

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./Include结尾的路径有 header (.h) 文件(它是一个 C 库),以/msc结尾的路径有一个 .lib 文件。 The.lib files are not static libraries, they are the interface files for some corresponding DLLs. .lib 文件不是 static 库,它们是一些相应 DLL 的接口文件。 Those DLL files are in C:\System32 .那些 DLL 文件位于C:\System32中。 There are also 32-bit versions in C:\SysWOW64 . C:\SysWOW64中也有 32 位版本。 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.现在,如果我从 cmd.exe 运行它,它工作正常。 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. 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.我已经对这个问题进行了大量的搜索和研究,但我有点被大多数人遇到相反问题的事实所诅咒,这意味着问题(我的另一个问题)是谷歌/DuckDuckGo 中出现的问题/StackOverflow/forum.qt.io/doc.qt.io 搜索。 That problem usually has to do with missing/misplaced Qt libraries.该问题通常与缺少/放错位置的 Qt 库有关。 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, eg this article .这个问题的答案通常以一个链接结束,该链接指向如何为 Windows 部署 Qt 项目的页面,例如这篇文章

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.此外,我还阅读了 Qt 中关于如何将库添加到您的项目中的这篇文章,但它并没有帮助我,但我可能遗漏了一些东西和/或做错了。

This might be something really dumb I'm missing and frankly I hope it is.这可能是我很想念的东西,坦率地说,我希望它是。 Thanks*10^6.谢谢*10^6。

TL;DR: The kit I was using to compile in Qt Creator had a different PATH set than my system PATH . TL;DR:我用来在 Qt Creator 中编译的套件与我的系统PATH设置的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.为了解决这个问题,我在cmd.exeecho %PATH并将所有与我尝试使用的驱动程序有关的内容复制到我在 Qt Creator 中使用的套件的PATH中。 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.正如@adrien-lerevat 所建议的,当从 Qt Creator 中运行时,我的可执行文件找不到它需要的一些 DLL。 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.总而言之,我在我的套件中定义了一个PATH (Qt 中的“套件”基本上是一个编译器、一个调试器和一些环境变量),它与我的正常系统不同,而不是超集小路。 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.你看,我从其他项目继承了这个套件用于其他目的,我没有意识到可以在其中设置PATH ,或者我正在设置一个。 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 .因此,要找到我为 Qt Creator 设置的PATH ,我转到Tools下拉菜单并选择Options... ,然后是Build & Run ,然后是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 .那应该给你一个东西清单,其中一个叫做Environment That should have a Change... button you can press:那应该有一个Change...按钮,您可以按下: 在此处输入图像描述

which should open a new window with all your environment stuff:这应该打开一个新的 window 与所有环境的东西: 在此处输入图像描述

(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.这是我找到PATH的地方,以及一些值得了解的库和包含路径。 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.所以现在我知道我的 Qt Creator PATH是什么,我打开cmd.exe并键入命令echo %PATH%以找出我的系统PATH是什么。 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.我抓取了与我正在使用的这些 VISA 驱动程序有关的所有内容(基本上是路径中包含VISA和/或IVI Foundation的所有内容)并将它们粘贴到我在 Qt Creator 中的PATH中。 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.我添加了c:\system32因为我知道这是 visa64.dll 所在的位置,这至少是一个顶级 DLL 我知道我需要。 Oddly enough, though, when I added just c:\system32 without all the VISA and IVI Foundation stuff, that didn't work.不过,奇怪的是,当我只添加c:\system32而没有所有VISAIVI Foundation的东西时,这不起作用。 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.所以,我不知道我添加到我的 Qt Creator 路径中的所有内容是否是必要的,因为我刚刚遇到了这个解决方案,但是一旦我削减列表以找出我真正需要的所有内容,我将在此处添加该信息. Just in case anyone else ever comes across this problem or is curious.以防万一其他人遇到这个问题或好奇。 And for the sake of completeness I suppose.为了完整起见,我想。 Okay thanks everyone;)好的谢谢大家;)

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

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