我正在尝试使用此处找到的 xtensor-python 示例。
我安装了 xtensor-python、pybind11 和 xtensor,还创建了一个 CMakeLists.txt。
从 /build 我跑了。$ cmake .. $ 制作
它构建没有错误。
我的 CMakeLists.txt 看起来像这样。
cmake_minimum_required(VERSION 3.15)
project(P3)
find_package(xtensor-python REQUIRED)
find_package(pybind11 REQUIRED)
find_package(xtensor REQUIRED)
我的 example.cpp 文件。
#include <numeric> // Standard library import for std::accumulate
#include "pybind11/pybind11.h" // Pybind11 import to define Python bindings
#include "xtensor/xmath.hpp" // xtensor import for the C++ universal functions
#define FORCE_IMPORT_ARRAY // numpy C api loading
#include "xtensor-python/pyarray.hpp" // Numpy bindings
double sum_of_sines(xt::pyarray<double>& m)
{
auto sines = xt::sin(m); // sines does not actually hold values.
return std::accumulate(sines.cbegin(), sines.cend(), 0.0);
}
PYBIND11_MODULE(ex3, m)
{
xt::import_numpy();
m.doc() = "Test module for xtensor python bindings";
m.def("sum_of_sines", sum_of_sines, "Sum the sines of the input values");
}
我的python文件。
import numpy as np
import example as ext
a = np.arange(15).reshape(3, 5)
s = ext.sum_of_sines(v)
s
但是我的 python 文件无法导入我的 example.cpp 文件。
File "examplepyth.py", line 2, in <module>
import example as ext
ImportError: No module named 'example'
我是cmake的新手。我想知道如何使用 CMakeLists.txt 正确设置这个项目