使用 opencv 和 visual studio 2019 安装 BGSlibrary 时出现问题

Problems installing BGSlibrary using opencv and visual studio 2019

所以我一周前问了一个类似的问题,并被发送到一个链接器错误的线程。 我现在已经通读了所有错误,并使用 cmake 对包括 OpenCV 在内的所有内容进行了全新安装。

问题是,当我现在正确添加项目时,我仍然遇到相同的链接器错误。我已经使用 cmake 构建了项目并将库文件添加到依赖项并将库目标添加到库目录。

https://github.com/andrewssobral/bgslibrary/tree/master 就是这个库,我按照 cmake 的步骤作为集成步骤,老实说,我只是迷失了我现在能做的事情。

如果有人可以逐步向我解释这一点,我将不胜感激,因为我很确定我做的一切都是正确的,但仍然得到错误的答案

我采取的所有步骤的照片: Initial Clone, Cmake, After a succesful build on release mode. Errors after implementing 1 line of code, The implementation that brings up the error

拜托,我真的迷路了。

PS: 在遵循 Drescherjms 的回答后,我重建了我的 OpenCV,但这次是在发布模式下(正如他们指出的那样,错误是它找不到那个 .lib 文件)。这解决了找不到的问题,但在构建时添加了在此之前不存在的新链接器错误负载。我会在这里粘贴其中的一些:

3>Main.obj : error LNK2019: unresolved external symbol "void __cdecl cv::destroyAllWindows(void)" (?destroyAllWindows@cv@@YAXXZ) referenced in function "public: static void __cdecl bgslibrary::Main::start(int,char const * *)" (?start@Main@bgslibrary@@SAXHPEAPEBD@Z)
3>Main.obj : error LNK2019: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z) referenced in function "public: static void __cdecl bgslibrary::Main::start(int,char const * *)" (?start@Main@bgslibrary@@SAXHPEAPEBD@Z)
3>VideoAnalysis.obj : error LNK2001: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z)
3>VideoCapture.obj : error LNK2001: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z)
3>PreProcessor.obj : error LNK2019: unresolved external symbol "public: class cv::Mat __cdecl cv::Mat::clone(void)const " (?clone@Mat@cv@@QEBA?AV12@XZ) referenced in function "public: class cv::Mat __cdecl bgslibrary::PreProcessor::getGrayScale(void)" (?getGrayScale@PreProcessor@bgslibrary@@QEAA?AVMat@cv@@XZ)
3>PreProcessor.obj : error LNK2019: unresolved external symbol "public: bool __cdecl cv::Mat::empty(void)const " (?empty@Mat@cv@@QEBA_NXZ) referenced in function "public: void __cdecl bgslibrary::PreProcessor::applyCanny(class cv::Mat const &,class cv::Mat &)" (?applyCanny@PreProcessor@bgslibrary@@QEAAXAEBVMat@cv@@AEAV34@@Z)
3>VideoCapture.obj : error LNK2001: unresolved external symbol "public: bool __cdecl cv::Mat::empty(void)const " (?empty@Mat@cv@@QEBA_NXZ)
3>PreProcessor.obj : error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBV_InputArray@1@@Z) referenced in function "public: void __cdecl bgslibrary::PreProcessor::process(class cv::Mat const &,class cv::Mat &)" (?process@PreProcessor@bgslibrary@@QEAAXAEBVMat@cv@@AEAV34@@Z)
3>VideoCapture.obj : error LNK2001: unresolved external symbol "void __cdecl cv::imshow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBV_InputArray@1@@Z)
3>PreProcessor.obj : error LNK2019: unresolved external symbol "void __cdecl cv::GaussianBlur(class cv::_InputArray const &,class cv::_OutputArray const &,class cv::Size_<int>,double,double,int)" (?GaussianBlur@cv@@YAXAEBV_InputArray@1@AEBV_OutputArray@1@V?$Size_@H@1@NNH@Z) referenced in function "public: void __cdecl bgslibrary::PreProcessor::process(class cv::Mat const &,class cv::Mat &)" (?process@PreProcessor@bgslibrary@@QEAAXAEBVMat@cv@@AEAV34@@Z)
3>PreProcessor.obj : error LNK2019: unresolved external symbol "void __cdecl cv::Canny(class cv::_InputArray const &,class cv::_OutputArray const &,double,double,int,bool)" (?Canny@cv@@YAXAEBV_InputArray@1@AEBV_OutputArray@1@NNH_N@Z) referenced in function "public: void __cdecl bgslibrary::PreProcessor::applyCanny(class cv::Mat const &,class cv::Mat &)" (?applyCanny@PreProcessor@bgslibrary@@QEAAXAEBVMat@cv@@AEAV34@@Z)
3>PreProcessor.obj : error LNK2019: unresolved external symbol "double __cdecl cv::threshold(class cv::_InputArray const &,class cv::_OutputArray const &,double,double,int)" (?threshold@cv@@YANAEBV_InputArray@1@AEBV_OutputArray@1@NNH@Z) referenced in function "public: void __cdecl bgslibrary::PreProcessor::applyCanny(class cv::Mat const &,class cv::Mat &)" (?applyCanny@PreProcessor@bgslibrary@@QEAAXAEBVMat@cv@@AEAV34@@Z)

并且还添加了这些警告以防它们有用

3>D:\GitRepos\opencv\build\lib\Release\opencv_gapi452.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
3>D:\GitRepos\opencv\build\lib\Release\opencv_highgui452.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
3>D:\GitRepos\opencv\build\lib\Release\opencv_ml452.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
3>D:\GitRepos\opencv\build\lib\Release\opencv_objdetect452.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
3>D:\GitRepos\opencv\build\lib\Release\opencv_photo452.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
3>D:\GitRepos\opencv\build\lib\Release\opencv_stitching452.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
3>D:\GitRepos\opencv\build\lib\Release\opencv_video452.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
3>D:\GitRepos\opencv\build\lib\Release\opencv_calib3d452.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
3>D:\GitRepos\opencv\build\lib\Release\opencv_dnn452.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
3>D:\GitRepos\opencv\build\lib\Release\opencv_features2d452.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'

对于在 Visual Studio 2019 年至少知道如何安装库和设置的人来说,问题是我在不同的配置中构建不同的版本。

我在 Debug Win32 中构建了 OpenCV,在 Release x64 中尝试构建了 BgsLibrary。在 drescherjm 谈到可能存在于 OpenCV 构建中的问题之后,我从头开始重建了 OpenCV,但这次是在 Release Win32 中(因为 bgslibrary 要求您在 Release 中构建)。之后,我也再次从头开始构建 bgslibrary,但这次也是在 Release Win32 中。之后它只是链接了所有的库并将 .dll 位置添加到我的路径并且它起作用了!

使其与 cmakeMSVC 一起使用的最简单方法是使用 vcpkg.

所以按照 vcpkg 的说明(构建此工具),使用该工具安装 OpneCV

然后在 CMakeLists.txt 中使用 find_package(OpenCV REQUIRED) 就差不多到家了。
有关详细信息,请参阅 OpenCV documentation 如何将 OpenCV 与 cmake.

一起使用

额外的收获是,如果有人将使用包管理器安装 OpenCV 并在 MacOS 上安装 OpenCV(有人正在使用 Home Brew),这也应该适用于 Linux。