我正在尝试使用xtensor-python示例找到here。
我安装了xtensor-python、pybind11和xtensor,还创建了一个CMakeLists.txt。
我从/build上运行。$ 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)我的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正确设置此项目
发布于 2019-08-22 22:03:38
推荐的方式是使用setup.py文件而不是cmake文件构建和安装。您可以使用cookie-cutter来获取为您生成的样板。
发布于 2019-08-24 02:14:18
嘿,我对xtensor-python不太确定,因为我没有用过它,但我可能会给你一些在蟒蛇环境中使用cmake构建pybind11的指点。您的Cmake.txt看起来有点不完整。对我来说,下面的设置是可行的:
在我的Anaconda-shell中,我使用了以下命令:
cmake -S <folder where Cmake.txt is> B <folder where Cmake.txt is\build> -G"Visual Studio 15 2017 Win64" 这会将所有链接放到子文件夹build中,因此实际的构建可以通过
cmake --build build必要的Cmake.txt如下所示。然后,创建的库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}) 如果你想要一个我恰好使用这个Cmake.txt文件的最小示例,它恰好在我在stackoverflow上发布的另一个问题中:pybind11 how to use custom type caster for simple example class
我希望这能帮助你作为第一个起点(我把EIGEN3留在里面,让你知道如何使用一个只有头的库来完成它。对于像OpenCV这样的实际库,您还需要target_link_libraries命令)。
https://stackoverflow.com/questions/57318921
复制相似问题