[英]Creating .so file using ctypes when opencv is included in C++ code
I am trying to create .so file of a c++ file(twocams.cpp) which includes main() and another C++ file (say abc.h).我正在尝试创建包含 main() 和另一个 C++ 文件(比如 abc.h)的 C++ 文件(twocams.cpp)的 .so 文件。 abc.c includes opencv. abc.c 包括 opencv。 while creating an object using ctypes,使用 ctypes 创建对象时,
g++ -fPIC -shared twocams.cpp -o twocams.so
Test = ctypes.cdll.LoadLibrary('/home/administrator/Desktop/project/twocams/twocams.so')
i am getting error as:- undefined symbol: _ZN2cv12VideoCapturersERNS_3MatE我收到错误为:- 未定义符号:_ZN2cv12VideoCapturersERNS_3MatE
How to solve this problem?如何解决这个问题呢? I want to use the c++ code in python.我想在python中使用c++代码。 Any alternative is acceptable.任何替代方案都是可以接受的。
When you create a shared object and want to use it, you have to link your artifact against all dependencies of this shared object.当您创建一个共享对象并想要使用它时,您必须将您的工件链接到该共享对象的所有依赖项。 Eg I create a libtwocams.so
of例如,我创建了一个libtwocams.so
#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.要使用此共享对象,我必须链接 libopencv_core、libopencv_videoio、libopencv_imgproc、libopencv_imgcodecs、libz 等。 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.另一种解决方案是将共享对象链接到依赖库。 Eg例如
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
.您可以使用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.如果 libA 依赖于 libB,那么您必须先链接 libA,然后链接 libB。
If you use opencv's shared library then you don't need to link all other dependencies.如果您使用 opencv 的共享库,则不需要链接所有其他依赖项。
Here is a step by step manual:这是分步手册:
Add repository bincrafters to conan将存储库 bincrafters 添加到 conan
conan remote add bincrafters https://api.bintray.com/conan/bincrafters/public-conan
Create:创建:
conanfile.txt: conanfile.txt:
[requires]
opencv/3.4.2@bincrafters/stable
[generators]
cmake
[options]
opencv:shared=True
CMakeLists.txt: 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:去build
和安装依赖项:
cd build
conan install .. --build missing
Build project with cmake:使用 cmake 构建项目:
cmake ..
cmake --build .
I had a similar issue, giving me an我有一个类似的问题,给我一个
OSError: /lib64/libarmadillo.so.9: undefined symbol: H5Ovisit
in python, when calling在 python 中,调用时
libCustCv = ctypes.cdll.LoadLibrary('./../so/opencv_cust.so')
To resolve this, I did the following:为了解决这个问题,我做了以下事情:
#include <opencv2/opencv.hpp>
by the specific ones: #include <opencv2/core/core.hpp>
and #include <opencv2/imgproc/imgproc.hpp>
在 cpp 文件中,我将一般导入#include <opencv2/opencv.hpp>
替换为特定的: #include <opencv2/core/core.hpp>
和#include <opencv2/imgproc/imgproc.hpp>
pkg-config --libs --cflags opencv
as arguments of the compilation command (which gave quite a list), I just used the specific flags: g++ -fPIC -shared -I/usr/include/opencv4 -lopencv_imgproc -lopencv_core -Wall -Wl,-soname,opencv_cust.so -o opencv_cust.so opencv_cust.cpp
我没有将pkg-config --libs --cflags opencv
的 bash 输出作为编译命令的参数(给出了相当多的列表),而是使用了特定的标志: g++ -fPIC -shared -I/usr/include/opencv4 -lopencv_imgproc -lopencv_core -Wall -Wl,-soname,opencv_cust.so -o opencv_cust.so opencv_cust.cpp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.