使用OpenCV与Visual Studio 2019安装BGSlibrary时遭遇链接器错误求助
解决BGS Library + OpenCV链接器错误的分步指南
我看你折腾这个链接器问题好一阵了——从最初找不到.lib文件,到重新编译OpenCV Release模式后又冒出一堆未解析外部符号,还有架构冲突的警告,确实挺闹心的!咱们先从最显眼的核心问题入手,一步步来解决:
1. 先搞定架构不匹配的关键问题(LNK4272警告)
你给出的警告信息明确显示:library machine type 'x86' conflicts with target machine type 'x64'——也就是说你的项目是x64架构,但使用的OpenCV库是**x86(32位)**版本,这直接会导致链接失败,很多未解析符号问题其实都是这个原因引发的。
解决步骤:
- 打开CMake重新编译OpenCV:
- 选择生成器时,一定要选对应你VS版本的Win64版本(比如
Visual Studio 17 2022 Win64),不能选默认的x86 - 配置时确保
CMAKE_BUILD_TYPE设为Release,和你的项目构建模式保持一致 - 编译完成后,找到x64 Release目录下的lib文件(路径一般是
opencv/build/x64/vc17/lib/Release这类)
- 选择生成器时,一定要选对应你VS版本的Win64版本(比如
2. 统一所有组件的架构和构建模式
接下来要确保BGS Library、你的项目、OpenCV三者的**架构(x64)和构建模式(Release)**完全统一:
- 重新运行CMake配置BGS Library:
- 指定新编译的x64 Release版OpenCV的路径,查看CMake输出日志,确认
OpenCV_FOUND显示为YES,且路径指向x64版本 - 生成BGS Library的项目文件后,选择x64 Release模式重新构建
- 指定新编译的x64 Release版OpenCV的路径,查看CMake输出日志,确认
- 你的项目也要保持x64 Release模式:
- 在VS中右键项目→属性→配置属性→常规,确认平台是
x64,配置是Release
- 在VS中右键项目→属性→配置属性→常规,确认平台是
3. 检查链接器依赖的正确性
现在要确保项目链接的是正确的OpenCV库文件:
- 打开项目属性→链接器→输入→附加依赖项,添加所有用到的OpenCV模块对应的lib文件,根据你的错误信息,至少需要这些:
(注意Release版本的lib不带opencv_core452.lib opencv_highgui452.lib opencv_imgproc452.lib opencv_videoio452.libd后缀,Debug版本才带) - 确认链接器→常规→附加库目录,指向的是x64 Release版OpenCV的lib文件夹,而不是之前的x86路径
4. 清理缓存并重新构建
最后一步,彻底清理所有缓存,避免残留的旧文件干扰:
- 删除你的项目、BGS Library、OpenCV的build文件夹
- 重新生成OpenCV、BGS Library,再重新构建你的项目
附:你遇到的错误信息参考
未解析外部符号错误(LNK2019/LNK2001)
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)
架构冲突警告(LNK4272)
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'
内容的提问来源于stack exchange,提问作者Georgie L.
相关产品推荐
相关产品推荐

