C++ 使用 OpenCV 库编译代码时出现链接错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4559945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:45:14  来源:igfitidea点击:

Linking errors when compiling code with OpenCV Libraries

c++opencvcompilation

提问by Kaigi

I'm trying to compile a sample program after installing Opencv with the command:

我正在尝试使用以下命令安装 Opencv 后编译示例程序:

g++ hello-world.cpp -o hello-world -I /usr/local/include/opencv -L /usr/local/lib -lm -lcv -lhighgui -lcvaux

however, I'm getting an error that says:

但是,我收到一条错误消息:

/usr/bin/ld: cannot find -lcv  
/usr/bin/ld: cannot find -lhighgui  
/uer/bin/ld: cannot find -lcvaux
collect2: ld returned 1 exit status

What do I need to do to correct this?? I installed opencv by downloading the latest stable version and using cmake to create the build files, then ran make install from the command line.

我需要做什么来纠正这个?我通过下载最新的稳定版本并使用 cmake 创建构建文件来安装 opencv,然后从命令行运行 make install。

Was there anything I may have missed?

有什么我可能错过的吗?

采纳答案by Ignacio Vazquez-Abrams

You need to add another -Largument specifying the actual location of the OpenCV libraries.

您需要添加另一个-L参数来指定 OpenCV 库的实际位置。

回答by anarchy99

UPDATED-

更新-

Better use this command:

最好使用此命令:

g++ opencv.cpp -o opencv -L `pkg-config --cflags --libs opencv`

The pkg-configcommand will locate the correct include and library for your source code.
For better handling with OpenCV programming go with an IDE like code::block.

pkg-config命令将为您的源代码找到正确的包含和库。
为了更好地处理 OpenCV 编程,请使用 IDE 之类的code::block.

Maybe this tutorial will help you in OpenCV programming with code::block:
How to Setup OpenCV for code :: block in Linux and Windows?

也许本教程将帮助您进行 OpenCV 编程code::block
如何在 Linux 和 Windows 中为代码设置 OpenCV :: 块?

回答by wildCat

Recently I started using OpenCV and I got similar problem and for me this works really well:

最近我开始使用 OpenCV 并且遇到了类似的问题,对我来说这非常有效:

-lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect

Hope it will solve your problem.

希望它能解决你的问题。