参考内容:
- Python使用ctypes调用C/C++的方法 \ 官方文档地址 \ python调用C++
- Linux下编译安装OpenCV(基于C++,生成so库供python调用)
- C++编译OpenCV生成动态库问题解决
- python利用ctypes的指针传入int型列表和string类型列表到c语言中
1 本人代码结构
2 修改cpp代码(其中input()是由我原来的main()函数改的)
3 生成so库 (gcc2so.sh)
g++ src/main.cpp src/wrink.cpp \
src/face_landmark.cpp src/face_t_shape.cpp src/detect_face.cpp \
-o libtest.so -fPIC -shared -lcurl -ldlib -llapack -lcblas -L. -std=c++11 \
`pkg-config --libs opencv --cflags opencv` -Wl,-rpath,./
可能会遇到问题 (libdlib.a recompile with -fPIC 问题)
/usr/bin/ld: dlib_build/libdlib.a(threads_kernel_shared.o): relocation R_X86_64_32 against `_ZN4dlib21threads_kernel_shared14thread_starterEPv' can not be used when making a shared object;
recompile with -fPIC
————————————————
原文链接及解决方法:https://blog.csdn.net/yanjiee/article/details/77896019
4. 在Python中调用.so (hello_so.py)
from ctypes import *
so = cdll.LoadLibrary
lib=so("./libtest.so")
# lib.hello_so_python()
# print("hello so")
s=bytes("./t4/", "utf-8")
dirImgs=c_char_p(s)
lib.input(dirImgs)