C++ 使用发布库时,OpenCV imread(filename) 在调试模式下失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9125817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV imread(filename) fails in debug mode when using release libraries
提问by Jav_Rock
I have some C++code and everything was working fine with OpenCVexcept the function imread(file)
. It was finding correctly the file and loading the name, but it wasn't loading any data.
我有一些C ++代码,一切工作正常使用OpenCV的除了功能imread(file)
。它正确地找到了文件并加载了名称,但没有加载任何数据。
Mat pattImage = imread(fileName, 0);
After some reaserch on the web I realized that I was in debug mode but with the release OpenCVlibraries, instead of the debug ones.
在网络上进行一些研究后,我意识到我处于调试模式,但使用的是OpenCV库,而不是调试库。
debug library: opencv_core231d.lib
release library: opencv_core231.lib
Though it is the tipical stupid error I thought this shouldn't have anything to do, the debug libraries are supposed to allow OpenCVcode debugging while the release libraries allow faster execution of the code, but I don't understand why imread was failing.
虽然这是典型的愚蠢错误,但我认为这不应该有任何关系,调试库应该允许OpenCV代码调试,而发布库允许更快地执行代码,但我不明白为什么 imread 失败。
Can anybody explain me the differences between debugand releaselibraries in OpenCVand why this error occurs?
任何人都可以向我解释OpenCV 中调试库和发布库之间的区别以及为什么会发生此错误?
Is it an OpenCVbug?
这是一个OpenCV错误吗?
回答by karlphillip
I'll never get tired of telling people that the C++ OpenCV interface for Windows has the wierdest bugs.
我永远不会厌倦告诉人们Windows 的 C++ OpenCV 接口有最奇怪的错误。
Write a small test using the C interface to check if it works or not (cvLoadImage()
, etc).
使用 C 接口编写一个小测试来检查它是否有效(cvLoadImage()
等)。
Update: now that you know that the C interface works properly, you can either go to the mailing list and report this bug there or dig into the code yourself to find why it fails.
更新:现在您知道 C 接口工作正常,您可以转到邮件列表并在那里报告此错误,或者自己深入研究代码以找出它失败的原因。
回答by Borneq
In release mode you must use release libraries, in debug mode - debug libraries. It is no bug.
在发布模式下,您必须使用发布库,在调试模式下 - 调试库。这不是错误。
回答by none
Had this problem using Qt (Qt Creator), linking the debug version of the respective library fixed it. This can be done automatically in the project configuration file (.pro):
使用 Qt (Qt Creator) 遇到了这个问题,链接相应库的调试版本修复了它。这可以在项目配置文件 (.pro) 中自动完成:
QTCreator .pro file: Setting LIBS path depending on DEBUG / RELEASE
回答by Mangirdas Skripka
Use FORWARD slash (/), instead of a backward slash (). Even in Windows!
使用正斜杠 (/),而不是反斜杠 ()。即使在 Windows 中!
Incorrect:
不正确:
cv::imread("C:\example.jpg");
Correct:
正确的:
cv::imread("C:/example/1.jpg");
回答by maidnl
In general it is perfecly legal to link "Debug" executable configuration against "Release" configuration library (why should not be as far as the symbols exported by the libraries are the same in Debug and in Release?). Unless (for some reasons) you don't want that "mixing" happen. It turns out that opencv developers decided to not allow such mixing and they perform such probihibition with a specific portion of code (something you can find in the file cvdef.h on release 3.4.4 line 54). That is not a C++ interface bug, but a "wanted" behaviour. You can find more information at https://github.com/opencv/opencv/pull/9161where this change has been documented.
一般来说,将“调试”可执行配置链接到“发布”配置库是完全合法的(为什么不应该尽可能地使库导出的符号在调试和发布中相同?)。除非(出于某些原因)您不希望“混合”发生。事实证明,opencv 开发人员决定不允许这种混合,并且他们对代码的特定部分执行这种禁止(您可以在 3.4.4 版本第 54 行的文件 cvdef.h 中找到某些内容)。这不是 C++ 接口错误,而是“想要的”行为。您可以在https://github.com/opencv/opencv/pull/9161上找到更多信息,其中记录了此更改。
回答by alvion
You can work around this issue by changing the runtime library of your Debug application from /MDd (multi-threaded DLL debug) to /MD (regular, release version of the multi-threaded DLL runtime).
您可以通过将调试应用程序的运行时库从 /MDd(多线程 DLL调试)更改为 /MD(多线程 DLL 运行时的常规发行版)来解决此问题。
Your code will still be unoptimized and easier to debug than a normal release mode, but you will lose some debugging information (for example for crashes within the C runtime). You also lose some debugging features like the debug heap, but if you don't know what that is then it won't affect you.
您的代码仍然未经优化并且比正常发布模式更容易调试,但您会丢失一些调试信息(例如 C 运行时中的崩溃)。您还会失去一些调试功能,例如调试堆,但如果您不知道那是什么,那么它不会影响您。
To do this work around, just go to Properties>C/C++/Code Generation and change "Runtime Library" from /MDd to /MD.
要解决此问题,只需转到“属性”>“C/C++/代码生成”并将“运行时库”从 /MDd 更改为 /MD。