简体   繁体   English

Cmake 找不到 boost_python

[英]Cmake could not find boost_python

I am trying to build this simple boost python demo from this link on my MacOS High Sierra.我正在尝试从我的 MacOS High Sierra 上的这个链接构建这个简单的 boost python 演示。

Following is the hello_ext.cpp :以下是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);
}

Following is the CmakeLists.txt :以下是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})

I figured I need to install python.我想我需要安装python。 Boost 1.69 was already installed and I did brew install boost-python which worked fine. Boost 1.69 已经安装,我确实brew install boost-python工作正常。 Doing a brew list | grep 'boost'做一个brew list | grep 'boost' brew list | grep 'boost' lists out boost and boost-python . brew list | grep 'boost'列出了boostboost-python

But, doing a cmake .. from a build directory complains the following:但是,从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.

What am I missing here?我在这里缺少什么?

From this document :这个文件

Note that Boost Python components require a Python version suffix (Boost 1.67 and later), eg python36 or python27 for the versions built against Python 3.6 and 2.7, respectively.请注意,Boost Python 组件需要 Python 版本后缀(Boost 1.67 和更高版本),例如分别针对 Python 3.6 和 2.7 构建的版本的 python36 或 python27。 This also applies to additional components using Python including mpi_python and numpy.这也适用于使用 Python 的其他组件,包括 mpi_python 和 numpy。 Earlier Boost releases may use distribution-specific suffixes such as 2, 3 or 2.7.较早的 Boost 版本可能使用特定于发行版的后缀,例如 2、3 或 2.7。 These may also be used as suffixes, but note that they are not portable.这些也可以用作后缀,但请注意它们不可移植。

The example you found was probably using an older version of Boost.您找到的示例可能使用的是旧版本的 Boost。 So, you may need to change this line:因此,您可能需要更改此行:

find_package(Boost COMPONENTS python27 REQUIRED)

To pass the right python version to find_package(Boost) , I suggest to extract it from the version of python found on the system.要将正确的 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)

The 3.6 on the first line is a minimum version, it finds the python38 boost module on my machine due to /usr/lib64/libpython3.8.so .第一行的 3.6 是最低版本,由于/usr/lib64/libpython3.8.so ,它在我的机器上找到了python38 boost 模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM