本文介绍了Ubuntu、opencv 和链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我前几天安装了Ubuntu,用apt-get安装了build_essentials、opencv、highgui等.g++是4.6.1,opencv好像是2.1.0..我没有从源码构建opencv..

I installed Ubuntu a few days ago, and used apt-get to install build_essentials, opencv, highgui, etc. g++ is 4.6.1, opencv appears to be 2.1.0.. I didn't build opencv from source..

我们有一些使用 opencv 功能的软件.让我们假设源文件名为 a.cpp 和 b.cpp.我编译为 a.o 和 b.o,然后将它们放入 .so 库(称为 libab.so).

We have some software that uses opencv functionality. Let's pretend the source files are named a.cpp and b.cpp. I compile to a.o and b.o and then put those into a .so library (call it libab.so).

最后,有一个包含 main 的文件(称为 z.cpp).我尝试从中构建一个可执行文件,但我收到大量 cv:: 东西的未定义引用"错误.我的链接线如下所示:

Finally, there's a file with main in it (call it z.cpp). I try to build an executable from it, but I get a ton of "undefined reference" errors to cv:: stuff. My link line looks something like this:

g++ -fPIC -g z.cpp -L../lib -lab -lml -lcvaux -lhighgui -lcv -lcxcore -o z.out

然后我得到未定义的引用错误(所有这些都是 cv:: stuff).

then I get the undefined reference errors (all of which are to cv:: stuff).

有趣的是,如果我直接链接到我的 .o 文件,它构建得很好.所以这个:

The interesting part is if I link directly with my .o files, it builds just fine. So this:

g++ -fPIC -g z.cpp a.o b.o -lml -lcvaux -lhighgui -lcv -lcxcore -o z.out

有效.

我读过的所有内容似乎都暗示这可能是一个链接线排序问题,但我已经尝试了所有排序排列并遇到了同样的问题,所以我真的不认为这是我的问题,但我仍然可以是错的.任何人都知道如何使用我的库构建它,以及如果我使用库中的完整 .o 文件成功构建但无法使用库本身构建,为什么会有不同的行为?

Everything I've read seems to imply this is likely a link-line-ordering issue, but I have tried all ordering permutations and get the same problem, so I really don't think its my issue, but I could still be wrong. Anyone have any ideas how I can get this to build with my library, and why it would act differently if I build with the complete set of .o files that are in the library successfully, but can't build with the library itself?

推荐答案

您可以将以下标志传递给 g++:

You can pass the following flag to g++:

`pkg-config --libs opencv`

例如:

g++ myprogram.cpp `pkg-config --libs opencv` -o myprogram

pkg-config 为编译器提供有关库的信息.

pkg-config give to the compiler the information about the library for you.

你可以看看:

/usr/local/lib/pkgconfig/opencv.pc

/usr/local/lib/pkgconfig/opencv.pc

此文件包含编译所需的所有相关信息:

This file contains all the relevant information for compilation:

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 2.3.1
Libs: -L${libdir} -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
Cflags: -I${includedir_old} -I${includedir_new}

这篇关于Ubuntu、opencv 和链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 17:21