c++ - Boost Python 基本示例不起作用

标签 c++ boost boost-python

尝试来自 Boost Python Quick Start 的最简单的 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),结果相同

Windows 7 32 位

C++ 编译器:Visual C++ 2015

Python 3.4.2,Boost 1.61

更新:已解决,见下文

最佳答案

我已将包含 Boost Python *.lib 和 *.dll 文件的目录添加到 PATH。它使示例工作

关于c++ - Boost Python 基本示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39160021/

相关文章:

c++ - 消除 `boost::xtime`中的 `boost::condition::timed_wait`

c++ - 使用引用参数 boost python 方法调用

python - 两个并发的 Python 进程可以在 Boost Python 中运行吗?

C++ 在参数名称之前需要 ')'

c++ - 当值为指针时尝试读取或写入 protected 内存

c++ - 防止实例化不是多态 C++ 的基类

c++ - Boost FFT 示例 - 编译时出错,这段代码在做什么?

c++ - 获取 qStylesheet 的属性

c++ - 有没有办法恢复嵌入在 boost mpl 引用中的原始模板模板类?

c++ - boost python 返回与 make_constructor 相同的实例