Hi I'm Frogatto Hi I'm Frogatto - 16 days ago 8
Linux Question

Creating a shared object which depends on other shared objects

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.

Answer

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.