参考官方文档
1. test.cpp
#include <boost/python.hpp>
#include <set>
#include <map>
#include <memory>
#include <iostream>
#include <chrono>
#include <stdlib.h>
#include <math.h>
#include <cstdio>
#include <boost/python/numpy.hpp>
#include <boost/python/overloads.hpp>
#include <opencv2/opencv.hpp>
#include <list>
#include <pthread.h>
namespace np = boost::python::numpy;
namespace py = boost::python;
using namespace boost::python;
using namespace std;
using namespace cv;
py::tuple test()
{
return py::make_tuple(123,"hello");
}
py::tuple test(int num)
{
return py::make_tuple(num,"hello");
}
class Test2
{
public:
int init();
int init(int num);
};
int Test2::init()
{
return -1;
}
int Test2::init(int num)
{
return num;
}
BOOST_PYTHON_MODULE(test_overloads)
{
py::tuple (*o1)() = test;
py::tuple (*o2)(int) = test;
int (Test2::*init1)() = &Test2::init;
int (Test2::*init2)(int) = &Test2::init;
def("test", o1);
def("test", o2);
class_<Test2>("Test2",init<>())
.def("init", init1)
.def("init", init2);
}
2. CMakeLists.txt
cmake_minimum_required(VERSION 3.6.0 FATAL_ERROR)
project(rtmp_streaming VERSION 1.0.0 DESCRIPTION "boost.python overlads test")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -std=c++14 -Wall")
set ( CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++14 -Wall -O0 -fno-omit-frame-pointer")
find_package(PythonInterp 3.6 REQUIRED)
find_package(PythonLibs 3.6 REQUIRED)
set(Python_ROOT_DIR "")
set(BOOST_PYTHONLIB python3)
find_package(Boost COMPONENTS ${BOOST_PYTHONLIB})
find_package(OpenCV REQUIRED)
include(CMakeToolsHelpers OPTIONAL)
include_directories(${PYTHON_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIR})
#SET_TARGET_PROPERTIES(test_overloads PROPERTIES PREFIX "")
add_library(test_overloads SHARED test.cpp)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
target_link_libraries(test_overloads ${OpenCV_LIBS} ${Boost_LIBRARIES} boost_numpy3 /usr/lib/x86_64-linux-gnu/libpython3.6m.so)
3. test_overloads.py
from test_overloads import test,Test2
print(test())
print(test(444))
t = Test2()
print(t.init())
print(t.init(666))