Created
October 2, 2014 12:21
-
-
Save termoshtt/045876cbdddcb4ab4772 to your computer and use it in GitHub Desktop.
C++でpythonを拡張する(Boost.NumPy) ref: http://qiita.com/termoshtt/items/0103803c40331c77c727
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'を省略するため |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git clone https://github.com/ndarray/Boost.NumPy | |
cd Boost.NumPy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd libs/numpy/doc | |
make |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
project/CMakeLists.txt | |
numpy.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake . | |
make |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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