OpenCv编译和链接错误

时间:2014-07-15 00:45:04

标签: c++ opencv

亲爱的朋友,请原谅我,如果我要问的问题听起来很愚蠢或非常基本。我现在尝试使用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)

2 个答案:

答案 0 :(得分:1)

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

如果您使用libc++,则两种约定都可以正常使用。出于某种原因,libstdc++无法识别较新的C ++函数。

答案 1 :(得分:0)

第一个代码是使用c库,而下面的代码使用c ++库... 1-确保为两者连接正确的opencv库 2 - 从错误代码中,我觉得错误来自混合32/64位库,尝试匹配它们。