创建一个依赖于其他共享对象的共享对象 [英] Creating a shared object which depends on other shared objects
问题描述
问题
我正在创建一个使用OpenCV共享对象的共享对象(称为 libA.so
) (例如 libopencv_core.so
)。
这是我的共享对象的代码:
#includeopencv2 / opencv.hpp
#includergbframe.h
#define API_EXPORT __attribute__((可见性(default)))
#define API_LOCAL __attribute__((visibility(hidden)))
using namespace cv;
API_EXPORT RGBFrame getFrame(int width,int height){
VideoCapture cap(0);
RGBFrame帧;
if(!cap.isOpened())
return frame;
//设置相机分辨率
cap.set(CV_CAP_PROP_FRAME_WIDTH,width);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,height);
frame.rows = height;
frame.cols = width;
frame.data = new uchar [frame.rows * frame.cols * 3];
Mat mat(frame.rows,frame.cols,CV_8UC3,frame.data);
//获取框架
帽子>>垫;
//将BGR转换为RGB
cv :: cvtColor(mat,mat,CV_BGR2RGB,3);
返回帧;
}
以下是我如何编译它:
gcc -shared -fPIC -Wl,-soname,libA.so -std = c ++ 11 -lpthread -lopencv_core -lopencv_video -lopencv_highgui -lopencv_imgproc -o libA。所以test.cpp
因此,从上面的行中我们知道 libA。所以
取决于 opencv_core
, opencv_video
等等。
它编译并链接正常,但当我 ldd libA.so
时,它会打印:
linux-vdso.so.1 => (0x00007ffc71b76000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1(0x00007f074cbaa000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6(0x00007f074c7e3000)
/lib64/ld-linux-x86-64.so.2(0x000055b29e8bd000)
这意味着我的库不依赖于OpenCV .so
文件。除此之外,当我在一个简单的程序中使用我的库时,整个程序将不会编译。我将在 libA.so
中使用OpenCV函数未定义引用
。
问题
我该如何构建我的 libA.so
它取决于OpenCV库?而当我在其他程序中使用它时,它会被编译并链接正常。
这可能是如何到的专有技术if-one-shared-library-depends-on-another-shared-library-or-not
TLDR是你应该把链接的库放在对象之后/源文件。
The Problem
I am creating a shared object (called libA.so
) which uses OpenCV shared objects (for example libopencv_core.so
).
Here's my shared object's code:
#include "opencv2/opencv.hpp"
#include "rgbframe.h"
#define API_EXPORT __attribute__ ((visibility ("default")))
#define API_LOCAL __attribute__ ((visibility ("hidden")))
using namespace cv;
API_EXPORT RGBFrame getFrame(int width, int height){
VideoCapture cap(0);
RGBFrame frame;
if(!cap.isOpened())
return frame;
// Set camera resolution
cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);
frame.rows = height;
frame.cols = width;
frame.data = new uchar[frame.rows * frame.cols * 3];
Mat mat(frame.rows, frame.cols, CV_8UC3, frame.data);
// Get a frame
cap >> mat;
// Convert BGR to RGB
cv::cvtColor(mat, mat, CV_BGR2RGB, 3);
return frame;
}
Also here's how I compile it:
gcc -shared -fPIC -Wl,-soname,libA.so -std=c++11 -lpthread -lopencv_core -lopencv_video -lopencv_highgui -lopencv_imgproc -o libA.so test.cpp
Therefore from the line above, we know that libA.so
depends on opencv_core
, opencv_video
and so forth.
It compiles and links fine, but when I ldd libA.so
, it prints:
linux-vdso.so.1 => (0x00007ffc71b76000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f074cbaa000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f074c7e3000)
/lib64/ld-linux-x86-64.so.2 (0x000055b29e8bd000)
meaning that my library doesn't depend on OpenCV .so
files. Apart from this, when I use my library in a simple program, the whole program won't compile. There will be a bunch of undefined reference
to OpenCV functions I have used in libA.so
.
The Question
How should I build my libA.so
such that it depends on OpenCV libraries? and when I use it in other programs, it will be compiled and linked fine.
This is probably a dup of how-to-know-if-one-shared-library-depends-on-another-shared-library-or-not
TLDR is that you should put linked libs after object/source files.
这篇关于创建一个依赖于其他共享对象的共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!