OpenCv编译和链接错误

OpenCv Compiling and Linking error

本文关键字:错误 链接 编译 OpenCv      更新时间:2023-10-16

亲爱的朋友,如果我要问的问题听起来很愚蠢或很基本,请原谅我。我现在正试图使用opencv三天,我发现很难在Qt和XCODE中编译代码,昨天我很幸运,在经过许多教程之后能够获得最基本的代码之一。然而,问题是在一些书中,我发现以这种方式编写的代码示例:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main( int argc, char** argv ) {
IplImage* img = cvLoadImage( "/Users/mughery/Desktop/1_s.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}

上面的代码工作得很好,它现在有问题了,但是当我测试下面的代码时,我得到了很多错误。我相信它们都是c++代码,它们应该做同样的事情。

    #include <iostream>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    using namespace cv;
    using namespace std;
int main()
{
//read an image
cv::Mat image = cv::imread("/Users/mughery/Desktop/1_s.jpg");
//create image window named "My Image"
cv::namedWindow("My Image");
cv::imshow("My image", image);
cv::waitKey(5000);
return 1;
}

当我运行第二个代码时,它说库没有找到,书都使用相同的库,所以有什么问题可以有人帮助请。我从第二个代码收到的错误是

Ld /Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Products/Debug/Open normal x86_64
cd /Users/mughery/Documents/ImageProgramtest/Open
export MACOSX_DEPLOYMENT_TARGET=10.9
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++      -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -L/Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Products/Debug -L/usr/local/lib -F/Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Products/Debug -filelist /Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Intermediates/Open.build/Debug/Open.build/Objects-normal/x86_64/Open.LinkFileList -mmacosx-version-min=10.9 -stdlib=libstdc++ -lopencv_highgui.2.4.9 -lopencv_core.2.4.9 -Xlinker -dependency_info -Xlinker /Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Intermediates/Open.build/Debug/Open.build/Objects-normal/x86_64/Open_dependency_info.dat -o /Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Products/Debug/Open
   Undefined symbols for architecture x86_64:
   "cv::namedWindow(std::string const&, int)", referenced from:
  _main in main.o
  "cv::imread(std::string const&, int)", referenced from:
  _main in main.o
   "cv::imshow(std::string const&, cv::_InputArray const&)", referenced from:
  _main in main.o
     ld: symbol(s) not found for architecture x86_64
     clang: error: linker command failed with exit code 1 (use -v to see invocation)

这两种约定对于在c++代码中使用OpenCV函数都是有效的。第一个代码片段使用C函数,而第二个代码片段使用c++函数。我复制了您的错误,当您在项目的构建设置下将 c++标准库从默认的libc++ (LLVM C++ standard library)更改为libstdc++ (GNU C++ standard library)时,就会发生这种情况。

如果您使用libc++,那么这两种约定都可以正常工作。由于某些原因,libstdc++不识别新的c++函数

第一个代码是使用c库,而下面的代码使用c++库…1-确保两者都链接了适当的openv库2 -从错误代码来看,在我看来,错误来自混合32/64位库,尝试匹配它们。

相关文章: