使用cmake和pybind11构建示例应用程序时未找到Python.h [英] Python.h not found while building sample application with cmake and pybind11

查看:70
本文介绍了使用cmake和pybind11构建示例应用程序时未找到Python.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用pybind11构建简单的应用程序,pybind已经通过cmake安装在我的Ubuntu系统中(并进行安装).我使用这个简单的cmake文件:

I want to build simple app with pybind11, pybind is already installed in my Ubuntu system with cmake (and make install). I use this simple cmake file:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(trt_cpp_loader )
find_package(pybind11 REQUIRED)
add_executable(trt_cpp_loader main.cpp)
set_property(TARGET trt_cpp_loader PROPERTY CXX_STANDARD 11)

这是main.cpp:

This is main.cpp:

#include <iostream>
#include <pybind11/embed.h>
namespace py = pybind11;

using namespace std;
int main(){return 0;}

建造它时,我得到:

In file included from /usr/local/include/pybind11/pytypes.h:12:0,
                 from /usr/local/include/pybind11/cast.h:13,
                 from /usr/local/include/pybind11/attr.h:13,
                 from /usr/local/include/pybind11/pybind11.h:44,
                 from /usr/local/include/pybind11/embed.h:12,
                 from /home/stiv/lpr/trt_cpp_loader/main.cpp:2:
/usr/local/include/pybind11/detail/common.h:112:10: fatal error: Python.h: No such file or directory
 #include <Python.h>
          ^~~~~~~~~~
compilation terminated.

如何解决此问题?(已经安装了python-dev和python3-dev,Python.h可用)

how can I fix this problem? (python-dev and python3-dev are already installed, Python.h is available)

推荐答案

遵循 Wenzel Jakob answer 我想举一个 CMakeLists.txt 的示例,以编译本教程:

Following the Wenzel Jakob's answer I want to put an example of CMakeLists.txt for compiling the example provided in this tutorial:

// example.cpp

#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");
}

# example.py

import example

print(example.add(1, 2))

# CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)
project(example)

find_package(pybind11 REQUIRED)
pybind11_add_module(example example.cpp)

现在在根目录下运行

cmake .
make

现在通过以下方式运行python代码

now run the python code by

python3 example.py

PS 我还在此处编写了一些说明,用于编译/安装pybind11 .

P.S. I have also written some instructions here for compiling/installing the pybind11.

这篇关于使用cmake和pybind11构建示例应用程序时未找到Python.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆