Skip to content

Instantly share code, notes, and snippets.

@termoshtt
Created October 2, 2014 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save termoshtt/045876cbdddcb4ab4772 to your computer and use it in GitHub Desktop.
Save termoshtt/045876cbdddcb4ab4772 to your computer and use it in GitHub Desktop.
C++でpythonを拡張する(Boost.NumPy) ref: http://qiita.com/termoshtt/items/0103803c40331c77c727
cmake_minimum_required(VERSION 2.8)
# set(CMAKE_VERBOSE_MAKEFILE 1)
find_package(Boost COMPONENTS python3 REQUIRED) # for python3
find_package(PythonLibs REQUIRED)
include_directories(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
add_library(mymodule SHARED numpy.cpp)
target_link_libraries(mymodule ${Boost_LIBRARIES} ${PYTHON_LIBRARY} boost_numpy)
set_target_properties(mymodule PROPERTIES PREFIX "") # 接頭辞'lib'を省略するため
git clone https://github.com/ndarray/Boost.NumPy
cd Boost.NumPy
cd libs/numpy/doc
make
project/CMakeLists.txt
numpy.cpp
cmake .
make
#include "boost/numpy.hpp"
namespace p = boost::python;
namespace np = boost::numpy;
np::ndarray new_zero1(unsigned int N) {
p::tuple shape = p::make_tuple(N);
np::dtype dtype = np::dtype::get_builtin<double>();
return np::zeros(shape, dtype);
}
BOOST_PYTHON_MODULE(mymodule) {
Py_Initialize();
np::initialize();
p::def("new_zero", new_zero1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment