【问题标题】:building cpp with pybind11使用 pybind11 构建 cpp
【发布时间】:2020-08-28 17:13:48
【问题描述】:

我是 pybind(以及 C++)的新手。我正在尝试遵循 pybind https://pybind11.readthedocs.io/en/stable/basics.html 上的教程,但我在构建它时遇到了麻烦。 我在桌面上有一个文件夹,里面有这个结构:

.
├── file.txt
├── include
│   └── pybind11
│       ├── attr.h
│       ├── buffer_info.h
│       ├── cast.h
│       ├── chrono.h
│       ├── common.h
│       ├── complex.h
│       ├── detail
│       │   ├── class.h   
│       │   ├── common.h
│       │   ├── descr.h
│       │   ├── init.h
│       │   ├── internals.h
│       │   └── typeid.h
│       ├── eigen.h
│       ├── embed.h
│       ├── eval.h
│       ├── functional.h
│       ├── iostream.h
│       ├── numpy.h
│       ├── operators.h
│       ├── options.h
│       ├── pybind11.h
│       ├── pytypes.h
│       ├── stl_bind.h
│       └── stl.h
├── prova.cpp
└── share
    └── cmake
        └── pybind11
            ├── FindPythonLibsNew.cmake
            ├── pybind11Config.cmake
            ├── pybind11ConfigVersion.cmake
            ├── pybind11Targets.cmake
            └── pybind11Tools.cmake

6 directories, 31 files

我的文件prova.cpp如下:

#include <pybind11/pybind11.h>

namespace py = pybind11;

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

PYBIND11_MODULE(prova, m) {
    m.def("add", &add, "A function which add two numbers");
}

现在,在教程中,他们说使用以下命令构建文件:

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

但我得到了这个输出:

c++: error: python3 -m pybind11 --includes: No such file or directory

现在,我通过 conda 安装了 python,并且我有一个包含所有所需包的环境。如果我在命令提示符下运行命令 python3 -m pybind11 --includes 我得到这个输出:

-I/home/luca/Programs/miniconda3/envs/fcg/include/python3.7m -I/home/luca/Programs/miniconda3/envs/fcg/lib/python3.7/site-packages/pybind11/include

显然,我必须在正确的 conda 环境中。因此,如果 c++ 无法使用正确的 conda 环境,我会措辞。 但是,即使我替换了直接在 c++ 命令中得到的输出,所以运行:

c++ -O3 -Wall -shared -std=c++11 -fPIC '-I/home/luca/Programs/miniconda3/envs/fcg/include/python3.7m -I/home/luca/Programs/miniconda3/envs/fcg/lib/python3.7/site-packages/pybind11/include' prova.cpp -o prova'python3-config --extension-suffix'

我得到另一个输出:

prova.cpp:1:10: fatal error: pybind11/pybind11.h: No such file or directory
#include <pybind11/pybind11.h>
      ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.

我真的不知道如何解决它。感谢那些可以提供帮助的人:)

【问题讨论】:

    标签: python c++ pybind11


    【解决方案1】:

    您需要在 c++ 行的前面附近包含 -I“包含 pybind11.h 文件的文件夹 pybind11 的路径”。

    -I"path" 告诉编译器在哪里可以找到头文件。

    【讨论】:

    • 像这样一样 c++ -I"/home/luca/Programs/miniconda3/envs/fcg/lib/python3.7/site-packages/pybind11/include" -O3 -Wall -shared -std=c++11 -fPIC 'python3 -m pybind11 --includes' prova.cpp -o prova'python3-config --extension-suffix' c++: error: python3 -m pybind11 --includes: No such file or目录
    • 也这样不行 c++ -I"/home/luca/Desktop/project/include/pybind11/" -O3 -Wall -shared -std=c++11 -fPIC 'python3 -m pybind11 --includes' prova.cpp -o prova'python3-config --extension-suffix' c++: error: python3 -m pybind11 --includes: No such file or directory
    • @luca-faraoni 尝试删除单引号。 c++ -O3 -Wall -shared -std=c++11 -fPIC -I/home/luca/Programs/miniconda3/envs/fcg/include/python3.7m -I/home/luca/Programs/miniconda3/envs/fcg /lib/python3.7/site-packages/pybind11/include prova.cpp -o prova'python3-config --extension-suffix
    【解决方案2】:

    第一步:找到pybuild11包含文件的位置:

    $ python3.8 -c "import pybind11;print(pybind11.get_include())"
    /usr/local/lib/python3.8/site-packages/pybind11/include
    

    步骤 2:添加路径以包含编译命令:

    $ c++ ...[bunch of stuff]... -I '/usr/local/lib/python3.8/site-packages/pybind11/include'  ...[bunch of stuff]...
    

    【讨论】:

      猜你喜欢
      • 2019-01-25
      • 1970-01-01
      • 2019-05-27
      • 2020-04-22
      • 1970-01-01
      • 2013-12-30
      • 2021-02-28
      • 2020-11-22
      • 2021-04-28
      相关资源
      最近更新 更多