如何设置cmakelists.txt文件以启动和运行Xtensor-python示例代码
How do I setup a CMakeLists.txt file to get xtensor-python sample code up and running
我正在尝试使用此处找到的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>
相关文章:
- 如何使用 ctypes 停止和重新启动从 Python 运行的C++代码
- 重新启动后,线程无法在 while 循环中再次运行
- 在GDB中:在启动程序中以代码1退出.在GDB之外运行良好
- 如何设置cmakelists.txt文件以启动和运行Xtensor-python示例代码
- 阻止进程在从服务启动时启动,并在某些进程关闭后继续运行服务。
- 如何判断从 CreateProcess 启动的进程是否仍在运行
- wxTimer 在启动时运行.需要在需要时启动它.(wxWidgets, Code::Blocks, C++)
- Windows中的主音量控制,在启动时运行
- 如何以与Explorer中的管理员的启动相同的方式从CPP运行程序
- 在启动C 应用程序时,如何检查MongoDB是否正在运行
- 启动/运行外部可执行文件并查看其输出日志
- 如何使用媒体基础在给定时间运行/启动线程
- 如何在运行时像可执行文件一样"启动"DLL?
- 您如何从已经运行的GUI应用程序中启动使用GUI的可执行文件
- 如何获取正在运行的进程的启动时间
- 如何使程序在启动时运行
- 系统会冻结如果我使用运行的守护程序重新启动或关闭,该守护程序可以使用Fanotify控制对文件的访问
- Ubuntu 上的 Qt+OpenCV 无法运行/调试:无法启动应用程序
- 如何检查程序是否由Windows服务启动和完美运行
- 在 boost::asio 中启动异步操作和运行io_service的正确顺序