我有一些用c ++编写的代码,我试图在python中使用而不再重写python中的完整代码,我使用Pybind11为它构建一个python模块.我试图通过以下教程在Microsoft Visual Studio 2015中实现此功能https://pybind11.readthedocs.io/en/stable/basics.html
我在视觉工作室做了一些事情.1)从https://codeload.github.com/pybind/pybind11/zip/master下载了Pybind11
2)解压缩文件
3)在visual studio中,启动了一个新的空C++项目.
4)在VC++目录> include目录中添加了我的python解释器include文件夹(C:/ python27/include)和Pybind11(C:/ Pybind11/include)
5)在链接器>输入>附加依赖项中添加了其他依赖项(C:\ Python27\libs\python27.lib)
6)要在Python中使用输出文件,我需要一个.pyd文件,所以我在这里修改了配置属性>常规>目标扩展:.pyd
7)将项目默认值>配置类型更改为动态库(.dll)
所以我能够构建我的项目并生成.pyd文件但是在导入这个模块时我收到以下错误:ImportError:动态模块没有定义init函数(initProject11)
我搜索了这个错误并得到了这个链接http://pybind11.readthedocs.io/en/stable/faq.html 但我找不到我的解决方案.
所以我正在寻找上述问题的解决方案.非常感谢提前.
这是我的CPP文件代码
#include <pybind11/pybind11.h>
int add(int i, int j) {
return i + j;
}
namespace py = pybind11;
PYBIND11_PLUGIN(example) {
py::module m("example", "pybind11 example plugin");
m.def("add", &add, "A function which adds two numbers");
return m.ptr();
}
Run Code Online (Sandbox Code Playgroud) 我设计了一个 C++ 系统,它从在单独线程中运行的过程调用用户定义的回调。简化后system.hpp如下所示:
#pragma once
#include <atomic>
#include <chrono>
#include <functional>
#include <thread>
class System
{
public:
using Callback = std::function<void(int)>;
System(): t_(), cb_(), stop_(true) {}
~System()
{
stop();
}
bool start()
{
if (t_.joinable()) return false;
stop_ = false;
t_ = std::thread([this]()
{
while (!stop_)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (cb_) cb_(1234);
}
});
return true;
}
bool stop()
{
if (!t_.joinable()) return false;
stop_ = true;
t_.join();
return true;
}
bool registerCallback(Callback cb)
{
if (t_.joinable()) return false; …Run Code Online (Sandbox Code Playgroud) 我正在尝试从包含main()使用的函数的C ++代码中调用python函数Pybind11。但是我发现很少有参考资料可用。现有的大多数文档都谈到了相反的方向,即从Python调用C ++。
有没有完整的示例说明如何执行此操作?我找到的唯一参考是:https : //github.com/pybind/pybind11/issues/30
但是它的信息很少。