-
首先下载python,并且将python配置到环境变量中,如果使用window的安装程序,可以勾选自动添加到环境变量。
-
下载pybind11源码,https://github.com/pybind/pybind11
-
使用cmake-gui工具配置vs工程,并生成include头文件目录和cmake目录。
(1) 在pybind11源码目录中创建build目录和install目录。
(2) 按照如下方式输入你的pybind11源码目录,build目录和install目录。然后点击Configure,Generate,Open Project按钮。
(3) 如果有报错看下是不是python版本太老了,如果没有报错,打开vs编译,安装。
(4) 安装成功后会在install目录下生成include和share目录。 -
使用pybind11
(1) 首先cmake配置pybind11。
cmake_minimum_required(VERSION 3.2)
project(wecat_3d_sdk)
set(CMAKE_BUILD_TYPE "RelWithDebInfo")
set(3RD_PATH ${PROJECT_SOURCE_DIR}/3rd)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/debug)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/bin/debug/)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/bin/debug/)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
file(GLOB_RECURSE HEAD_LIST ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h ${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp)
file(GLOB_RECURSE SOURCES_LIST ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
set(SOURCE_FILE
${HEAD_LIST}
${SOURCES_LIST}
)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/src
${3RD_PATH}/pybind11/include
)
if(WIN32)
set(pybind11_DIR ${3RD_PATH}/pybind11/share/cmake/pybind11)
set(3RD_LIB
)
else()
endif()
find_package(pybind11 REQUIRED)
pybind11_add_module(${PROJECT_NAME}_py ${SOURCE_FILE})
(2) 绑定c++中的结构体和类到python环境中。
//c++结构体和类 如下结构是个人项目中自定义结构,使用者可随意替换。
#include <string>
#include <memory>
#include <vector>
#include <functional>
struct FrameData
{
double x;
double z;
int i;
int peak_width;
unsigned int encoder;
};
using FrameFun = std::function<void(const std::vector<FrameData>&)>;
class SDK_API Scanner
{
public:
Scanner();
~Scanner();
bool connect(const std::string& ip, int time_out = 100);
bool disconnect();
bool capture_one_frame(std::vector<FrameData>& frame_data);
bool start_capture(FrameFun fun = nullptr);
bool stop_capture();
bool save_txt(const std::string& path, const std::vector<FrameData>& frame_data);
class Impl;
std::unique_ptr<Impl> impl;
};
//绑定方式
#include "Scanner.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
namespace py = pybind11;
PYBIND11_MODULE(LineScanner, m)
{
m.doc() = "line scanner sdk";
py::class_<FrameData>(m, "FrameData")
.def(py::init<>())
.def_readwrite("x", &FrameData::x)
.def_readwrite("z", &FrameData::z)
.def_readwrite("i", &FrameData::i)
.def_readwrite("peak_width", &FrameData::peak_width)
.def_readwrite("encoder", &FrameData::encoder);
py::bind_vector<std::vector<FrameData> >(m, "FrameDataVector");
py::class_<Scanner>(m, "Scanner")
.def(py::init<>())
.def("connect", &Scanner::connect)
.def("disconnect", &Scanner::disconnect)
.def("capture_one_frame", &Scanner::capture_one_frame)
.def("save_txt", &Scanner::save_txt);
}
标签:set,CMAKE,SOURCE,window,pybind11,使用,include,DIR
From: https://www.cnblogs.com/yangzixiong/p/17927517.html