首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置CMakeLists.txt文件以启动并运行xtensor-python示例代码

如何设置CMakeLists.txt文件以启动并运行xtensor-python示例代码
EN

Stack Overflow用户
提问于 2019-08-02 09:43:45
回答 2查看 439关注 0票数 0

我正在尝试使用xtensor-python示例找到here

我安装了xtensor-python、pybind11和xtensor,还创建了一个CMakeLists.txt。

我从/build上运行。$ cmake ..$ make

而且它的构建没有错误。

我的CMakeLists.txt是这样的。

代码语言:javascript
复制
cmake_minimum_required(VERSION 3.15)
project(P3)

find_package(xtensor-python REQUIRED)
find_package(pybind11 REQUIRED)
find_package(xtensor REQUIRED)

我的example.cpp文件。

代码语言:javascript
复制
#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文件。

代码语言:javascript
复制
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文件。

代码语言:javascript
复制
  File "examplepyth.py", line 2, in <module>
    import example as ext
ImportError: No module named 'example'

我是cmake的新手。我想知道如何使用CMakeLists.txt正确设置此项目

EN

回答 2

Stack Overflow用户

发布于 2019-08-22 22:03:38

推荐的方式是使用setup.py文件而不是cmake文件构建和安装。您可以使用cookie-cutter来获取为您生成的样板。

票数 0
EN

Stack Overflow用户

发布于 2019-08-24 02:14:18

嘿,我对xtensor-python不太确定,因为我没有用过它,但我可能会给你一些在蟒蛇环境中使用cmake构建pybind11的指点。您的Cmake.txt看起来有点不完整。对我来说,下面的设置是可行的:

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

代码语言:javascript
复制
cmake -S <folder where Cmake.txt is> B <folder where Cmake.txt is\build> -G"Visual Studio 15 2017 Win64" 

这会将所有链接放到子文件夹build中,因此实际的构建可以通过

代码语言:javascript
复制
cmake --build build

必要的Cmake.txt如下所示。然后,创建的库TEST位于子文件夹debug\Build中

代码语言:javascript
复制
#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 how to use custom type caster for simple example class

我希望这能帮助你作为第一个起点(我把EIGEN3留在里面,让你知道如何使用一个只有头的库来完成它。对于像OpenCV这样的实际库,您还需要target_link_libraries命令)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57318921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档