简体   繁体   中英

OpenCv Compiling and Linking error

Dear Friend please forgive me if the question i am about to ask sound stupid or very basic. I am trying to use the opencv for three days now and i am finding it very hard to compile the code in both Qt and XCODE, yesterday i was luck enough after gong through many tutorial was able to get one of the most basic codes working. The problem however is in some books i find example of codes written in this way:

#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" );
}

The above code works very fine, it has now problem what so ever, however when i test the code bellow i get a plenty of error. i believe they are both C++ code and they are supposedly doing the same thing.

    #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;

}

When i run the second code it says library not found, the books are using the same library for both so what's wrong can some one help please. The error i recive from the second code is

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)

Both conventions are valid to use OpenCV functions in C++ code. The first code snippet uses C functions while the second one uses C++ functions . I reproduced your bug and it happens when you have changed the C++ Standard Library from the default of libc++ (LLVM C++ standard library) to libstdc++ (GNU C++ standard library) under the Build Settings of your Project.

If you use libc++ then both conventions work fine. For some reason, libstdc++ does not recognize the newer C++ functions.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM