为什么包含OpenCV的DNN模块会在macOS上给出链接器错误?

时间:2019-07-29 09:41:02

标签: c++ macos opencv

我尝试使用OpenCV的DNN模块编译C ++代码,但出现链接器错误。该项目使用了许多其他OpenCV模块,没有任何问题,因此我想可以很好地添加标题和源代码,并且仅DNN模块有问题。添加链接器标志以确保它也具有-lopencv_dnn_objdetect和-lopencv_dnn。没关系,从源代码编译安装有Homebrew的OpenCV。我使用Xcode 11 beta,我团队的其他成员在Windows上使用Visual Studio,他们使用相同的代码没有任何问题。

我得到的错误:

Ld /Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Products/Debug/OpenCV normal x86_64 (in target: OpenCV)
    cd /Users/hordon/Desktop/GreenFox/OpenCV
    /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -target x86_64-apple-macos10.14 -isysroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -L/Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Products/Debug -L/usr/local/Cellar/opencv/4.1.0_2/lib -L/usr/local/Cellar/sqlite/3.28.0/lib -L/usr/local/Cellar/tesseract/4.0.0_1/lib -L/usr/local/Cellar/leptonica/1.78.0/lib -F/Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Products/Debug -filelist /Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Intermediates.noindex/project.build/Debug/OpenCV.build/Objects-normal/x86_64/OpenCV.LinkFileList -Xlinker -object_path_lto -Xlinker /Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Intermediates.noindex/project.build/Debug/OpenCV.build/Objects-normal/x86_64/OpenCV_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -stdlib=libc++ -I/usr/local/Cellar/opencv/4.1.0_2/include/opencv4/opencv -I/usr/local/Cellar/opencv/4.1.0_2/include/opencv4 -L/usr/local/Cellar/opencv/4.1.0_2/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core -ltesseract -lsqlite3 -Xlinker -dependency_info -Xlinker /Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Intermediates.noindex/project.build/Debug/OpenCV.build/Objects-normal/x86_64/OpenCV_dependency_info.dat -o /Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Products/Debug/OpenCV
Undefined symbols for architecture x86_64:
  "cv::dnn::dnn4_v20180917::blobFromImage(cv::_InputArray const&, cv::_OutputArray const&, double, cv::Size_<int> const&, cv::Scalar_<double> const&, bool, bool, int)", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
  "cv::dnn::dnn4_v20180917::Net::forward(cv::_OutputArray const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
  "cv::dnn::dnn4_v20180917::Net::setInput(cv::_InputArray const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, double, cv::Scalar_<double> const&)", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
  "cv::dnn::dnn4_v20180917::Net::~Net()", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
  "cv::dnn::dnn4_v20180917::readNet(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
  "cv::dnn::dnn4_v20180917::NMSBoxes(std::__1::vector<cv::RotatedRect, std::__1::allocator<cv::RotatedRect> > const&, std::__1::vector<float, std::__1::allocator<float> > const&, float, float, std::__1::vector<int, std::__1::allocator<int> >&, float, int)", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

如果它包含DNN模块,例如this sample code from OpenCV,我尝试的每个代码都会抛出此链接器错误。

1 个答案:

答案 0 :(得分:0)

与此同时,我找到了解决方案,所以如果有人遇到相同问题,我将其留在这里。如果使用Homebrew的brew install opencv命令安装了OpenCV,则不会编译DNN模块。您必须从可以使用

实现的源进行重建

brew reinstall opencv --build-from-source

命令。由于此时我的搜索路径已正确添加,因此我只需要再次构建我的项目即可,而没有任何错误。