如何使用OpenCV创建python库.so

时间:2019-05-15 17:03:10

标签: python c++ opencv boost cmake

我在项目中使用OpenCV。我的目标是在python中创建最终脚本。如果在Windows上没有问题,并且我的.pyd文件在Windows上可以正常工作,那么在Linux上我会看到问题。

据我了解,所有库均已成功定位。文件运行良好。但是,当您尝试构建时,一切都会中断。很多错误“未定义的引用”。

#include "HeadrToDll.h"

namespace py = boost::python;
//namespace fs = std::filesystem;

py::list LineOfListGeneral(string mask_adress)//, bool del_file = 1)
{
    LineOfList lines;
    int er = 0;
    vector<int> res_c = lines.General(mask_adress, er);
    //if (del_file) fs::remove(mask_adress);
    py::list res_py;
    if (er == 0)
    {
        for (int i = 0; i < res_c.size(); i++)
            res_py.append(res_c[i]);
    }
    else res_py.append(er);
    return res_py;
}

BOOST_PYTHON_MODULE(Work)
{
    py::def("LineOfListGeneral", LineOfListGeneral);
}
#define BOOST_PYTHON_STATIC_LIB
#include <boost/python.hpp>
#include <Python.h>
//#include <filesystem>
#include "ObjectList.h" //my work that uses OpenCV

我使用两个Makfile。向下:

cmake_minimum_required (VERSION 3.8)

#SET(CMAKE_CXX_STANDARD 17)
find_package (Python COMPONENTS Interpreter Development REQUIRED)
FIND_PACKAGE(Boost REQUIRED)
find_package( OpenCV REQUIRED )
message(${Python_LIBRARIES})
include_directories( ${OpenCV_INCLUDE_DIRS} ${Python_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} )
LINK_LIBRARIES(${Python_LIBRARIES} ${Boost_LIBRARIES} ${OpenCV_LIBS})
message(${OpenCV_LIBS})
#add_executable (LineDetector2.1 "LineDetector2.1.cpp" "LineDetector2.1.h")
add_executable (LineDetector2.1 "SourceToDll.cpp" "HeadrToDll.h")
#add_library(LineDetector2 STATIC "SourceToDll.cpp" "HeadrToDll.h")
#target_link_libraries( LineDetector2.1 ${OpenCV_LIBS} )
#target_link_libraries( LineDetector2.1 ${Python_LIBRARIES})
target_link_libraries(LineDetector2.1 LINK_PUBLIC ${Boost_LIBRARIES}
        ${Python_LIBRARIES}
        ${OpenCV_LIBS})

然后向上:

cmake_minimum_required (VERSION 3.8)

project ("LineDetector2.1")

add_subdirectory ("LineDetector2.1")

我也尝试过

g++ -c -fPIC -I/usr/include/python2.7 -I/usr/local/include/opencv4 SourceToDll.cpp -o SourceToDll.o
g++ -shared -Wl,-soname,SourceToDll.so -o Work.so  SourceToDll.o -L/usr/local/lib -lpython2.7 -lboost_python

但是当尝试导入python

ImportError: ./Work.so: undefined symbol: _ZN2cv5CannyERKNS_11_InputArrayERKNS_12_OutputArrayEddib

0 个答案:

没有答案