Ubuntu,opencv和链接 [英] Ubuntu, opencv and linking

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

问题描述

几天前我安装了Ubuntu,并使用apt-get安装了build_essentials,opencv,highgui等.g ++是4.6.1,opencv似乎是2.1.0.

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 ::东西有关.)

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文件集进行构建,却又无法与该库本身一起构建,为什么它的行为会有所不同? /p>

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和链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆