如何设置cmakelists.txt文件以启动和运行Xtensor-python示例代码

How do I setup a CMakeLists.txt file to get xtensor-python sample code up and running

本文关键字:运行 启动 Xtensor-python 代码 文件 何设置 设置 txt cmakelists      更新时间:2023-10-16

我正在尝试使用此处找到的Xtensor-python示例。

我已经安装了Xtensor-python,pybind11和Xtensor,还创建了一个cmakelists.txt。

从/构建我跑了。$ cmake ..$ make

它构建没有错误。

我的cmakelists.txt看起来像这样。

cmake_minimum_required(VERSION 3.15)
project(P3)
find_package(xtensor-python REQUIRED)
find_package(pybind11 REQUIRED)
find_package(xtensor REQUIRED)

我的示例.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文件无法导入我的示例.cpp文件。

  File "examplepyth.py", line 2, in <module>
    import example as ext
ImportError: No module named 'example'

我是Cmake的新手。我想知道如何使用cmakelists正确设置这个项目。txt

推荐的方法是使用setup.py文件而不是cmake构建和安装。您可以使用Cookie-Cutter为您生成样板。

嘿,我不确定Xtensor-python,因为我没有使用过它,但是我可能会给您一些指针,以在Anaconda环境中使用Cmake构建Pybind11。您的cmake.txt看起来有点不完整。对我来说,以下设置作品:

在我的anaconda-shell中,我使用以下命令:

cmake -S <folder where Cmake.txt is> B <folder where Cmake.txt isbuild> -G"Visual Studio 15 2017 Win64" 

将所有链接到子文件夹构建中,因此可以通过

完成实际建筑物
cmake --build build

必要的cmake.txt看起来像下面。然后,创建的库测试位于子文件夹质量 build

#minimum version of cmake
cmake_minimum_required(VERSION 2.8.12)
#setup project
project(TEST)
#load the libraries
find_package(pybind11 REQUIRED)
set(EXTERNAL_LIBRARIES_ROOT_PATH  <Filepath where my external libraries are at>)
set(EIGEN3_INCLUDE_DIR ${EXTERNAL_LIBRARIES_ROOT_PATH}/eigen-eigen-c753b80c5aa6) 
#get all the files in the folder
file(GLOB SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/*.h
    ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp   
)
#include the directories
include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_INCLUDE_DIRS} ${EIGEN3_INCLUDE_DIR})
pybind11_add_module(TEST MODULE ${SOURCES})
#in some cases need to link the libraries 
#target_link_libraries(TEST PUBLIC ${OpenCV_LIBRARIES} ${Boost_LIBRARIES})  

如果您想要一个我完全使用此cmake.txt文件的工作最小示例,则它恰好是我在stackoverflow上发布的另一个问题:pybind11如何将自定义类型施法者用于简单示例class

>

希望这是一个第一个起点(我将eigen3留在内部,以便您了解它如何使用仅标题库进行。对于像OpenCV这样的实际库,您还需要target_link_libraries命令。p>