When you create a shared object and want to use it, you have to link your artifact against all dependencies of this shared object. E.g. I create a libtwocams.so
of
#include <opencv2/videoio.hpp>
void test() {
cv::VideoCapture v;
cv::Mat m;
v >> m;
}
To use this shared object I have to link against libopencv_core, libopencv_videoio, libopencv_imgproc, libopencv_imgcodecs, libz and some more. I compile my program with
g++ main.cpp -o main -ltwocams -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_videoio -lz -lwebp -lpthread -ltiff -lpng
Another solution is to link the shared object against the dependent libraries. E.g.
g++ -fPIC -shared twocams.cpp -o -ltwocams -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_videoio -lz -lwebp -lpthread -ltiff -lpng
creates a shared library that makes the loader load all dependencies. You can check the dependencies with ldd
.
The order of the libraries is important. If libA has a dependency to libB then you have to link against libA and then libB.
If you use opencv's shared library then you don't need to link all other dependencies.
Here is a step by step manual:
Install conan
Install cmake
Add repository bincrafters to conan
conan remote add bincrafters https://api.bintray.com/conan/bincrafters/public-conan
Create:
- conanfile.txt
- CMakeLists.txt
- src/twocams.cpp
- build/
conanfile.txt:
[requires]
opencv/3.4.2@bincrafters/stable
[generators]
cmake
[options]
opencv:shared=True
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(twocams)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_library(twocams SHARED src/twocams.cpp src/twocams.h)
target_link_libraries(twocams ${CONAN_LIBS})
Go to build
and install dependencies:
cd build
conan install .. --build missing
Build project with cmake:
cmake ..
cmake --build .