在使用 cmake 和 pybind11 构建示例应用程序时找不到 Python.h
Python.h not found while building sample application with cmake and pybind11
我想用pybind11构建简单的应用程序,pybind已经安装了我的Ubuntu系统中cmake(并进行安装(。我使用这个简单的cmake文件:
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)
这是主要的.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 可用(
您需要使用pybind11_add_module
命令(请参阅 https://pybind11.readthedocs.io/en/stable/compiling.html#building-with-cmake(作为创建扩展模块的默认情况。
如果目标确实是在可执行文件中嵌入Python,那么您有责任将python标头和库显式添加到CMake中的编译器/链接器命令中。(请参阅有关如何执行此操作 https://pybind11.readthedocs.io/en/stable/compiling.html#embedding-the-python-interpreter(
在Wenzel Jakob的回答之后,我想举一个CMakeLists.txt
示例来编译本教程中提供的示例:
// 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 代码
python3 example.py
附言我还在这里写了一些编译/安装pybind11
的说明。
也许只是安装 Python 标头?例如,在 Ubuntu 上,您可以安装sudo apt-get install python-dev
(或python3-dev
或pythonX.Y-dev
(软件包。 这可以解决这个问题。
相关文章:
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 将库链接到另一个应用程序时,CMAKE 找不到库
- Qt应用程序找不到第三方DLL并崩溃
- CMake导出的库-在客户端应用程序中找不到include/Lib路径
- Qt视频流应用程序:找不到qt.mediaplayer的服务
- 在使用 cmake 和 pybind11 构建示例应用程序时找不到 Python.h
- 在使用 bazel 构建一个简单的应用程序时,得到错误在 '/usr/lib/java/jdk1.8.0_74/bin/java' 找不到 java
- Qt应用程序在运行时找不到共享库
- 程序找不到回车符或换行符。退货超出范围
- (Qt 5.6)切换到"Release"模式时程序找不到.txt文件
- 交叉编译用于ARM的MongoDB C++驱动程序.找不到Boost库
- SFML2 应用程序找不到共享对象
- LLVM JIT编译的程序找不到外部函数
- C++程序找不到提升
- 服务器应用程序看不到具有相对路径的文件
- 为什么应用程序找不到这个libSDL2_image-2.0.so.0库?-Ubuntu 14.04
- 服务加载程序找不到服务提供程序类,即使类与 META-INF/services 位于同一 JAR 文件中也是如此
- c++应用程序找不到com dll,因为编译器使用不正确的guid生成.tlh文件
- boost.log std::异常格式化程序找不到运算符<< 自己的命名空间中的重载
- SFML 程序找不到 libGLEW