首页 > 解决方案 > Windows/Cygwin - 无法使用 pybind11 - 出错

问题描述

我想在一个项目中集成实用工具 pybind11。我的工作环境是窗户。

要安装pybind11,有这个页面:Pybind First setps

我在 windows 和几天 (3) 下尝试了几次,使用 Eclipse,然后使用 Python 和 Mingw。然后使用 Visual Studio

经过这一切,我尝试了 Linux,并在 1 小时内来到了这个例子:

#include <pybind11 / pybind11.h>

int add (int i, int j) {
    return i + j;
}

PYBIND11_MODULE (example, m) {
    m.doc () = "pybind11 example plugin"; // optional module docstring
    m.def ("add", & add, "A function which adds two numbers");
}

c++ -O3 -Wall -shared -std = c ++ 11 -fPIC示例.cpp python3 -m pybind11 --includes-o 示例python3-config --extension-suffix

用测试除了Python。

在此之后,我决定使用 cygwin 让它成功

这是我的位置:

easy_install-3.6 pip
python3 -m pip install pytest
python3 -m pip install pybind11
python3 -m pip install python-config

然后在pybind11-master下

mkdir build
cd build
cmake ..
make check -j 4

我有两个警告和一个注释:

警告:“pybind11 :: buffer :: buffer (pybind11 :: handle, bool)”已弃用:使用 reinterpret_borrow () 或 reinterpret_steal () [-Wdeprecated-declarations]

链接时,这是一个错误,但有很多。

/tmp/ccqNl6ln.ltrans0.ltrans.o::(.text+0x12f16):未定义引用“pybind11 :: index_error :: index_error () [clone .lto_priv.4841] [clone .lto_priv.4883]”

pybind11 确实是该项目的一个分项。但我不能在 Windows 上使用它。

有没有人用cygwin编译过pybind11?

非常感谢你

标签: c++python-3.xwindowscygwinpybind11

解决方案


使用 pip 安装 3 个模块后

python3 -m pip install pytest
python3 -m pip install pybind11
python3 -m pip install python-config

以下示例有效

#include <pybind11/pybind11.h>

int add (int i, int j) {
    return i + j;
}

PYBIND11_MODULE (example, m) {
    m.doc () = "pybind11 example plugin"; // optional module docstring
    m.def ("add", & add, "A function which adds two numbers");
}

如果编译为:

c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix) -lpython3.6m

推荐阅读