gpt4 book ai didi

c++ - OpenCv 编译和链接错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:58:07 24 4
gpt4 key购买 nike

亲爱的 friend ,如果我要问的问题听起来很愚蠢或非常基础,请原谅我。我现在尝试使用 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 functions而第二个使用 C++ functions .我重现了您的错误,当您将 C++ 标准库 从默认的 libc++(LLVM C++ 标准库) 更改为 libstdc++(GNU C++ 标准库)时,它就会发生) 在项目的 Build Settings 下。

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

关于c++ - OpenCv 编译和链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24748307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com