Cmake 找不到 boost_python
Cmake could not find boost_python
我正在尝试在我的 MacOS High Sierra 上从 this link 构建这个简单的提升 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.
我在这里错过了什么?
Note that Boost Python components require a Python version suffix (Boost 1.67 and later), e.g. python36 or python27 for the versions built against Python 3.6 and 2.7, respectively. This also applies to additional components using Python including mpi_python and numpy. Earlier Boost releases may use distribution-specific suffixes such as 2, 3 or 2.7. These may also be used as suffixes, but note that they are not portable.
您找到的示例可能使用了旧版本的 Boost。因此,您可能需要更改此行:
find_package(Boost COMPONENTS python27 REQUIRED)
要将正确的 python 版本传递给 find_package(Boost)
,我建议从系统上找到的 python 版本中提取它。
find_package(PythonLibs 3.6 REQUIRED)
# Extract major/minor python version
string(REPLACE "." ";" VERSION_LIST ${PYTHONLIBS_VERSION_STRING})
list(GET VERSION_LIST 0 PYTHONLIBS_VERSION_MAJOR)
list(GET VERSION_LIST 1 PYTHONLIBS_VERSION_MINOR)
find_package(Boost COMPONENTS python${PYTHONLIBS_VERSION_MAJOR}${PYTHONLIBS_VERSION_MINOR} REQUIRED)
第一行的 3.6 是最低版本,由于 /usr/lib64/libpython3.8.so
.
它在我的机器上找到了 python38
boost 模块
我正在尝试在我的 MacOS High Sierra 上从 this link 构建这个简单的提升 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.
我在这里错过了什么?
Note that Boost Python components require a Python version suffix (Boost 1.67 and later), e.g. python36 or python27 for the versions built against Python 3.6 and 2.7, respectively. This also applies to additional components using Python including mpi_python and numpy. Earlier Boost releases may use distribution-specific suffixes such as 2, 3 or 2.7. These may also be used as suffixes, but note that they are not portable.
您找到的示例可能使用了旧版本的 Boost。因此,您可能需要更改此行:
find_package(Boost COMPONENTS python27 REQUIRED)
要将正确的 python 版本传递给 find_package(Boost)
,我建议从系统上找到的 python 版本中提取它。
find_package(PythonLibs 3.6 REQUIRED)
# Extract major/minor python version
string(REPLACE "." ";" VERSION_LIST ${PYTHONLIBS_VERSION_STRING})
list(GET VERSION_LIST 0 PYTHONLIBS_VERSION_MAJOR)
list(GET VERSION_LIST 1 PYTHONLIBS_VERSION_MINOR)
find_package(Boost COMPONENTS python${PYTHONLIBS_VERSION_MAJOR}${PYTHONLIBS_VERSION_MINOR} REQUIRED)
第一行的 3.6 是最低版本,由于 /usr/lib64/libpython3.8.so
.
python38
boost 模块