用pybind11构建cpp

2024-09-26 22:53:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我是pybind(也是C++)的新手。我正试图遵循pybindhttps://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

显然,我必须在正确的康达环境中。所以我在关注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.

我真的不知道怎么修理它。感谢那些能够提供帮助的人:)


Tags: cmakeaddhomeincludecpppython3fileint

热门问题