The Correct Cmakelists.Txt File to Call a Maxon Libarary in a Python Script Using Pybind11

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

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.

pybind11 cmake example cannot find the main function

I think you are mixing two different approaches up.

Embedding specifically refers to embed the python interpreter into an existing executable. The document that you refer to make it (or try to) quite clear.

What it means is that you should have a C/C++ executable from which you can execute python code (either inside a file or as a string).

Now that this is out of the way, look inside your built directory and you will find a cmake_example binary. Run it and you will see the print. You cannot directly import this built module from within standard python interpreter, rather it is available inside the file invoked from the custom executable, cmake_example in this case.

You can also run example.py by changing the code as following:

int main()
{
py::scoped_interpreter guard{};

py::eval_file("example.py");
}

add_cuda_library outputing: Unresolved extern function

Since you mentioned you are using directly LANGUAGES CUDA. You may just use add_library() instead of add_cuda_library() as it is supported by cmake as an first languague. I guess some project using older version cmake(<3.8?) would need something like CUDA_ADD_LIBRARY CUDA_ADD_EXECUTABLE. But as a first language, you can just regard it as a normal c project.

passing a list of strings from python to C through pybind11

Below I've reformatted the previous example code where I used C++ constructs, to only use C and pybind11 ones.

#include <pybind11/pybind11.h>#include <stdio.h>
#if PY_VERSION_HEX < 0x03000000#define MyPyText_AsString PyString_AsString#else#define MyPyText_AsString PyUnicode_AsUTF8#endif
namespace py = pybind11;
int run(py::object pyargv11) {int argc = 0;char** argv = NULL;
PyObject* pyargv = pyargv11.ptr();if (PySequence_Check(pyargv)) { Py_ssize_t sz = PySequence_Size(pyargv); argc = (int)sz;
argv = (char**)malloc(sz * sizeof(char*)); for (Py_ssize_t i = 0; i < sz; ++i) { PyObject* item = PySequence_GetItem(pyargv, i); argv[i] = (char*)MyPyText_AsString(item); Py_DECREF(item); if (!argv[i] || PyErr_Occurred()) { free(argv); argv = nullptr; break; } }}
if (!argv) { //fprintf(stderr, "argument is not a sequence of strings\n"); //return;
if (!PyErr_Occurred()) PyErr_SetString(PyExc_TypeError, "could not convert input to argv"); throw py::error_already_set();}
for (int i = 0; i < argc; ++i) fprintf(stderr, "%s\n", argv[i]);
free(argv);
return 0;}
PYBIND11_MODULE(example, m) {m.def("run", &run, "runs the example");}


Related Topics



Leave a reply



Submit