Boost Python 基本示例不起作用
Boost Python basic example does not work
中最简单的 Hello World 示例
#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
使用以下 CMake 在 Windows 上编译项目:
cmake_minimum_required(VERSION 3.2)
project(hello_ext CXX)
set(TARGET hello_ext)
set(BOOST_MIN_VERSION "1.61.0")
set(Boost_ADDITIONAL_VERSIONS "1.61.0" "1.61")
set(BOOST_ROOT ${MY_BOOST_DIR})
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_USE_MULTITHREADED ON)
find_package(PythonLibs 3.4 REQUIRED )
find_package(Boost 1.61.0 COMPONENTS python REQUIRED)
file(GLOB SOURCES *.cpp)
include_directories(${INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
python_add_module(${TARGET} ${SOURCES})
target_link_libraries(${TARGET} ${Boost_PYTHON_LIBRARY} ${PYTHON_LIBRARIES})
模块编译成功 hello_ext.pyd。尝试从位于同一目录中的 Python 脚本访问它:
import hello_ext
print(hello_ext.greet())
得到如下执行结果:
python3 test_cpp.py
Import Error: DLL load failed: The specified module could not be found
此外,尝试将 hello_ext.pyd 放入 Python DLL 目录 (C:/Python34/DLLs),结果相同
Windows7个32位
C++ 编译器:Visual C++ 2015
Python 3.4.2,提升 1.61
更新:已解决,见下文
我已将包含 Boost Python *.lib 和 *.dll 文件的目录添加到 PATH。它使示例工作
这是针对 mac 用户的,因为我尝试了上述解决方案,但使用 osx el capitan 时它对我不起作用。我有两个 CMakeLists.txt 可以工作,但是,我认为它们不是基于 https://gitlab.kitware.com/cmake/cmake/issues/16335 的问题。
PROJECT(example)
set(CMAKE_CXX_STANDARD 11)
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
cmake_policy(SET CMP0042 NEW)
set(EIGEN_DIR "/usr/local/include/eigen3/" )
set(PYTHON_INCLUDE_DIRS "//anaconda/include/python2.7")
set(PYTHON_LIBRARY "//anaconda/lib/libpython2.7.dylib")
find_package(Boost 1.66.0 COMPONENTS python)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS} ${EIGEN_DIR} ${PYTHON_INCLUDE_DIRS} include)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
add_library (yay MODULE src/example_ext.cpp)
target_link_libraries(yay ${Boost_LIBRARIES} ${PYTHON_LIBRARY})
endif()
PROJECT(example)
set(CMAKE_CXX_STANDARD 11)
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
cmake_policy(SET CMP0042 NEW)
set(EIGEN_DIR "/usr/local/include/eigen3/" )
find_package(Boost 1.66.0 COMPONENTS python3)
if(Boost_FOUND)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(PythonLibs REQUIRED)
include_directories(${Boost_INCLUDE_DIRS} ${EIGEN_DIR} ${PYTHON_INCLUDE_DIRS} include)
add_library (yay SHARED src/example_ext.cpp)
target_link_libraries(yay ${Boost_LIBRARIES} ${PYTHON_LIBRARY})
endif()
#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
使用以下 CMake 在 Windows 上编译项目:
cmake_minimum_required(VERSION 3.2)
project(hello_ext CXX)
set(TARGET hello_ext)
set(BOOST_MIN_VERSION "1.61.0")
set(Boost_ADDITIONAL_VERSIONS "1.61.0" "1.61")
set(BOOST_ROOT ${MY_BOOST_DIR})
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_USE_MULTITHREADED ON)
find_package(PythonLibs 3.4 REQUIRED )
find_package(Boost 1.61.0 COMPONENTS python REQUIRED)
file(GLOB SOURCES *.cpp)
include_directories(${INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
python_add_module(${TARGET} ${SOURCES})
target_link_libraries(${TARGET} ${Boost_PYTHON_LIBRARY} ${PYTHON_LIBRARIES})
模块编译成功 hello_ext.pyd。尝试从位于同一目录中的 Python 脚本访问它:
import hello_ext
print(hello_ext.greet())
得到如下执行结果:
python3 test_cpp.py
Import Error: DLL load failed: The specified module could not be found
此外,尝试将 hello_ext.pyd 放入 Python DLL 目录 (C:/Python34/DLLs),结果相同
Windows7个32位
C++ 编译器:Visual C++ 2015
Python 3.4.2,提升 1.61
更新:已解决,见下文
我已将包含 Boost Python *.lib 和 *.dll 文件的目录添加到 PATH。它使示例工作
这是针对 mac 用户的,因为我尝试了上述解决方案,但使用 osx el capitan 时它对我不起作用。我有两个 CMakeLists.txt 可以工作,但是,我认为它们不是基于 https://gitlab.kitware.com/cmake/cmake/issues/16335 的问题。
PROJECT(example)
set(CMAKE_CXX_STANDARD 11)
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
cmake_policy(SET CMP0042 NEW)
set(EIGEN_DIR "/usr/local/include/eigen3/" )
set(PYTHON_INCLUDE_DIRS "//anaconda/include/python2.7")
set(PYTHON_LIBRARY "//anaconda/lib/libpython2.7.dylib")
find_package(Boost 1.66.0 COMPONENTS python)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS} ${EIGEN_DIR} ${PYTHON_INCLUDE_DIRS} include)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
add_library (yay MODULE src/example_ext.cpp)
target_link_libraries(yay ${Boost_LIBRARIES} ${PYTHON_LIBRARY})
endif()
PROJECT(example)
set(CMAKE_CXX_STANDARD 11)
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
cmake_policy(SET CMP0042 NEW)
set(EIGEN_DIR "/usr/local/include/eigen3/" )
find_package(Boost 1.66.0 COMPONENTS python3)
if(Boost_FOUND)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(PythonLibs REQUIRED)
include_directories(${Boost_INCLUDE_DIRS} ${EIGEN_DIR} ${PYTHON_INCLUDE_DIRS} include)
add_library (yay SHARED src/example_ext.cpp)
target_link_libraries(yay ${Boost_LIBRARIES} ${PYTHON_LIBRARY})
endif()