【发布时间】:2023-03-28 03:05:02
【问题描述】:
我正在尝试在我的 MacOS High Sierra 上从 this link 构建这个简单的 boost python 演示。
下面是hello_ext.cpp:
#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
下面是CmakeLists.txt:
cmake_minimum_required(VERSION 3.5)
# Find python and Boost - both are required dependencies
find_package(PythonLibs 2.7 REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)
# Without this, any build libraries automatically have names "lib{x}.so"
set(CMAKE_SHARED_MODULE_PREFIX "")
# Add a shared module - modules are intended to be imported at runtime.
# - This is where you add the source files
add_library(hello_ext MODULE hello_ext.cpp)
# Set up the libraries and header search paths for this target
target_link_libraries(hello_ext ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
target_include_directories(hello_ext PRIVATE ${PYTHON_INCLUDE_DIRS})
我想我需要安装 python。 Boost 1.69 已经安装,我做了brew install boost-python,效果很好。执行brew list | grep 'boost' 会列出boost 和boost-python。
但是,从build 目录执行cmake .. 会报错:
Could not find the following Boost libraries:
boost_python
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to
the directory containing Boost libraries or BOOST_ROOT to the location
of Boost.
我在这里错过了什么?
【问题讨论】:
-
也许在您的 CMakeList.txt 中发布与 boost_python 相关的部分,以便我们仔细查看?我怀疑 boost_python 库不在 LD_LIBRARY_PATH 或您的 MacOS 版本中。
-
代码取自我提到的链接。我现在发布了问题中的所有代码,从而使其成为一个完整的代码示例。
-
运行
cmake --trace或--trace-expand对于调试 CMake 逻辑非常有用。在 CMake 库存脚本中找到错误消息,然后将代码与跟踪中的相应位置进行比较,看看发生了什么。 -
您可以尝试改用
find_package(Boost COMPONENTS python27 REQUIRED)吗? -
find_package(Boost COMPONENTS python27 REQUIRED)修复了它:D。伟大的。谢谢。看起来我应该在要求 cmake 定位 python 时编写正确的 python 版本
标签: python c++ boost-python