简体   繁体   English

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

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

I'm trying to use xtensor-python example found here . 我正在尝试使用在此处找到的xtensor-python示例。

I have xtensor-python, pybind11, and xtensor installed and also created a CMakeLists.txt. 我安装了xtensor-python,pybind11和xtensor,还创建了CMakeLists.txt。

from /build I ran. 从/ build我运行。 $ cmake .. $ make $ cmake .. $ make

and it builds without errors. 它的构建没有错误。

My CMakeLists.txt looks like this. 我的CMakeLists.txt看起来像这样。

cmake_minimum_required(VERSION 3.15)
project(P3)

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

My example.cpp file. 我的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");
}

My python file. 我的python文件。

import numpy as np
import example as ext

a = np.arange(15).reshape(3, 5)
s = ext.sum_of_sines(v)
s

But my python file can't import my example.cpp file. 但是我的python文件无法导入example.cpp文件。

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

I am new to cmake. 我是cmake的新手。 I would like to know how to set this project up properly with CMakeLists.txt 我想知道如何使用CMakeLists.txt正确设置此项目

The recommended way is to build and install with a setup.py file instead of cmake. 推荐的方法是使用setup.py文件而不是cmake进行构建和安装。 You can use the cookie-cutter to get the boilerplate generated for you. 您可以使用切饼机为您生成样板。

Hey I am not sure about xtensor-python since I have not used it, but I might give you some pointers for building pybind11 with cmake within an Anaconda environment. 嘿,我不确定xtensor-python,因为我没有使用过它,但我可能会给您一些在Anaconda环境中使用cmake构建pybind11的指导。 Your Cmake.txt looks a bit incomplete. 您的Cmake.txt看起来有点不完整。 For me the following set-up works: 对我来说,以下设置适用于:

In my Anaconda-shell I use the following commands: 在我的Anaconda-shell中,我使用以下命令:

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

which puts all the linking into the subfolder build, so the actual building can be done via 将所有链接放入子文件夹构建中,因此可以通过以下方式完成实际构建

cmake --build build

The necessary Cmake.txt looks something like below. 必要的Cmake.txt如下所示。 The created library TEST is then located in the subfolder debug\\Build 然后,已创建的库TEST位于子文件夹debug \\ 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})  

If you want a working minimal example for which I use exactly this Cmake.txt file, it happens to be in another question I posted here on stackoverflow: pybind11 how to use custom type caster for simple example class 如果您想要一个完全使用此Cmake.txt文件的最小工作示例,它恰好是我在此处在stackoverflow上发布的另一个问题: pybind11如何为简单的示例类使用自定义类型转换程序

Hope this helps as a first starting point (I left the EIGEN3 inside as to give you an idea how it is done with a header-only library. For actual libraries like OpenCV you need the target_link_libraries command in addition). 希望这可以作为第一个起点(我将EIGEN3留在里面是为了让您了解如何使用仅标头的库。对于像OpenCV这样的实际库,您还需要target_link_libraries命令)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 pybind11 在 Python 脚本中调用 MAXON 库的正确 CMakeLists.txt 文件 - The correct CMakeLists.txt file to call a MAXON libarary in a Python script using pybind11 结合 CMakeLists.txt、boost-python 和 python setuptools - Combining CMakeLists.txt, boost-python with python setuptools 使用 CMakeLists.txt 构建 C 库,同时打包 python 库 - Build C library with CMakeLists.txt while packaging python library 如何在我的 Python 代码中添加和打印 txt 文件? - How do i add and print a txt file in my Python code? cmake 错误“源似乎不包含 CMakeLists.txt” - cmake error 'the source does not appear to contain CMakeLists.txt' 如何在 python .py 运行代码中输入文件 - How do I input a file in python .py running code 如何修复 CMakeLists.txt 中的 CMake 错误:生成器 NMake Makefiles 不支持平台规范,但已指定平台 x64 - how to fix CMake Error in CMakeLists.txt: Generator NMake Makefiles does not support platform specification, but platform x64 was specified Python。 我如何从txt文件中拆分()? - Python. how do i do split() from txt file? 从 Windows 批处理文件运行时,如何从 exe 中获取 Python sys.exit() 代码? - How do I get the Python sys.exit() code from an exe when running it from a Windows batch file? 如何从 Python 的数据集中获取第二个样本,而不从第一个样本中获取重复? - How I do I get a second sample from a dataset in Python without getting duplication from a first sample?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM