Python.h not found while building sample application with cmake and pybind11

前端 未结 2 1723
情话喂你
情话喂你 2020-12-19 20:51

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_         


        
相关标签:
2条回答
  • 2020-12-19 20:54

    You'll want to use the pybind11_add_module command (see https://pybind11.readthedocs.io/en/stable/compiling.html#building-with-cmake) for the default case of creating an extension module.

    If the goal is indeed to embed Python in an executable, it is your reponsibility to explicitly add python headers & libraries to the compiler/linker commands in CMake. (see https://pybind11.readthedocs.io/en/stable/compiling.html#embedding-the-python-interpreter on how to do that)

    0 讨论(0)
  • 2020-12-19 21:04

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

    and

    # example.py
    
    import example
    
    print(example.add(1, 2))
    

    and

    # CMakeLists.txt
    
    cmake_minimum_required(VERSION 2.8.12)
    project(example)
    
    find_package(pybind11 REQUIRED)
    pybind11_add_module(example example.cpp)
    

    now in the root run

    cmake .
    make
    

    now run the python code by

    python3 example.py
    

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

    0 讨论(0)
提交回复
热议问题