我正在try 更新用于构建的Docker映像中的Boost版本.由于最新版本的Boost不能通过APT用于较旧的Ubuntu版本(在我的例子中是18.04LTS),我不得不从源代码构建Boost.我在CMake中遇到了一些奇怪的行为,仅当从源代码构建Boost时,而不是通过apt-get install libboost-all-dev安装它.

在从源文件(Dockerfile中为BUILD_BOOST_FROM_SOURCE=1)构建Boost时,CMake发现"错误的"Python版本:

#5 563.5 -- Found Boost: /usr/local/include (found version "1.65.1")  
#5 564.1 -- Found Python: /usr/bin/python2.7 (found version "2.7.17") found components: Interpreter Development Development.Module Development.Embed 

在安装Boost Via apt-get install libboost-all-dev(Dockerfile中的BUILD_BOOST_FROM_SOURCE=0)时,我得到了所需的配置:

#5 88.70 -- Found Boost: /usr/include (found version "1.65.1")  
#5 89.16 -- Found Python: /usr/bin/python3.6 (found version "3.6.9") found components: Interpreter Development Development.Module Development.Embed 

下面是我的Dockerfile的精简版本,它再现了这个问题,并在最后运行CMake来判断结果.我已经将Build Boost版本更改为通过APT安装的完全相同的版本,该问题对于较新的版本仍然存在.

FROM --platform=$BUILDPLATFORM ubuntu:bionic

ENV DEBIAN_FRONTEND noninteractive
ENV TZ America/New_York
ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8
ENV LANGUAGE en_US.UTF-8

ARG BOOST_VERSION=1.65.1  # adjusted to be equal to the one installed via apt-get
ENV BUILD_BOOST_FROM_SOURCE=1  # build from source or install via apt-get

RUN /bin/bash <<EOF

set -euxo pipefail

# Install package dependencies
apt-get update
apt-get install -y --no-install-recommends \
    build-essential \
    software-properties-common \
    autoconf \
    automake \
    libtool \
    pkg-config \
    ca-certificates \
    libssl-dev \
    wget \
    git \
    curl \
    gnupg \
    language-pack-en \
    locales \
    locales-all \
    vim \
    gdb \
    valgrind

# install python
apt-get install -y --no-install-recommends \
    python \
    python-dev \
    python3 \
    python3-pip \

apt-get clean

# perform the rest in the /tmp dir
cd /tmp

if [ "$BUILD_BOOST_FROM_SOURCE" -eq 1 ]; then \
  # build and install the specific boost version from source given by the BOOST_VERSION ARG
  wget https://boostorg.jfrog.io/artifactory/main/release/${BOOST_VERSION}/source/boost_$(echo ${BOOST_VERSION} | tr . _).tar.bz2 && \
    tar --bzip2 -xf boost_$(echo ${BOOST_VERSION} | tr . _).tar.bz2 && \
    cd boost_$(echo ${BOOST_VERSION} | tr . _) && \
    ./bootstrap.sh --prefix=/usr/local && \
    ./b2 install; \
else \
  # Install boost simply via apt-get
  apt-get install -y --no-install-recommends \
    libboost-all-dev; \
fi

# Use kitware's CMake repository for an up-to-date version and install cmake
curl -sSf https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | apt-key add -
apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main'
apt-get install -y --no-install-recommends cmake

# check the boost and python versions found by cmake
echo -e 'cmake_minimum_required(VERSION 3.12)
project(JustCheckingSomeVersions)
find_package(Boost)
find_package(Python COMPONENTS Interpreter Development)
' > CMakeLists.txt

cmake .

EOF

我try 以各种方式更改安装顺序,并将--with-python=/usr/bin/python3传递给bootstrap.sh.所有这些措施都没有帮助,我也没有 idea ,也不太了解这里所涉及的错综复杂的情况,无法取得进展.如何确保CMake在此Docker镜像中从源代码构建Boost时仍能找到Python3.x?

编辑: 多亏了Qyriads comment,我能够进一步追踪到CMake没有找到3.6版本的Python.h头文件.库和可执行文件分别在/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6m.so/usr/bin/python3.6找到.CMAKE_FIND_DEBUG_MODE的输出显示如下:

#5 564.4 CMake Debug Log at /usr/share/cmake-3.25/Modules/FindPython/Support.cmake:2963 (find_path):
#5 564.4   find_path called with the following settings:
#5 564.4 
#5 564.4     VAR: _Python_INCLUDE_DIR
#5 564.4     NAMES: "Python.h"
#5 564.4     Documentation: Path to a file.
#5 564.4     Framework
#5 564.4       Only Search Frameworks: 0
#5 564.4       Search Frameworks Last: 0
#5 564.4       Search Frameworks First: 0
#5 564.4     AppBundle
#5 564.4       Only Search AppBundle: 0
#5 564.4       Search AppBundle Last: 0
#5 564.4       Search AppBundle First: 0
#5 564.4     CMAKE_FIND_USE_CMAKE_PATH: 1
#5 564.4     CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
#5 564.4     CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 0
#5 564.4     CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 0
#5 564.4     CMAKE_FIND_USE_INSTALL_PREFIX: 1
#5 564.4 
#5 564.4   find_path considered the following locations:
#5 564.4 
#5 564.4     /usr/include/python3.6m/Python.h
#5 564.4     /usr/include/Python.h
#5 564.4     /usr/Python.h
#5 564.4 
#5 564.4   The item was not found.
#5 564.4 
#5 564.4 Call Stack (most recent call first):
#5 564.4   /usr/share/cmake-3.25/Modules/FindPython.cmake:540 (include)
#5 564.4   CMakeLists.txt:5 (find_package)
#5 564.4 
#5 564.4 CMake Debug Log at /usr/share/cmake-3.25/Modules/FindPython/Support.cmake:2975 (find_path):
#5 564.4   find_path called with the following settings:
#5 564.4 
#5 564.4     VAR: _Python_INCLUDE_DIR
#5 564.4     NAMES: "Python.h"
#5 564.4     Documentation: Path to a file.
#5 564.4     Framework
#5 564.4       Only Search Frameworks: 0
#5 564.4       Search Frameworks Last: 0
#5 564.4       Search Frameworks First: 0
#5 564.4     AppBundle
#5 564.4       Only Search AppBundle: 0
#5 564.4       Search AppBundle Last: 0
#5 564.4       Search AppBundle First: 0
#5 564.4     CMAKE_FIND_USE_CMAKE_PATH: 1
#5 564.4     CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
#5 564.4     CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 1
#5 564.4     CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1
#5 564.4     CMAKE_FIND_USE_INSTALL_PREFIX: 1
#5 564.4 
#5 564.4   find_path considered the following locations:
#5 564.4 
#5 564.4     /usr/local/sbin/Python.h
#5 564.4     /usr/local/bin/Python.h
#5 564.4     /usr/sbin/Python.h
#5 564.4     /usr/bin/Python.h
#5 564.4     /sbin/Python.h
#5 564.4     /bin/Python.h
#5 564.4     /usr/local/include/x86_64-linux-gnu/Python.h
#5 564.4     /usr/local/include/Python.h
#5 564.4     /usr/local/Python.h
#5 564.4     /usr/include/x86_64-linux-gnu/Python.h
#5 564.4     /usr/include/Python.h
#5 564.4     /usr/Python.h
#5 564.4     /include/x86_64-linux-gnu/Python.h
#5 564.4     /include/Python.h
#5 564.4     /usr/X11R6/include/x86_64-linux-gnu/Python.h
#5 564.4     /usr/X11R6/include/Python.h
#5 564.4     /usr/X11R6/Python.h
#5 564.4     /usr/pkg/include/x86_64-linux-gnu/Python.h
#5 564.4     /usr/pkg/include/Python.h
#5 564.4     /usr/pkg/Python.h
#5 564.4     /opt/include/x86_64-linux-gnu/Python.h
#5 564.4     /opt/include/Python.h
#5 564.4     /opt/Python.h
#5 564.4     /usr/include/X11/Python.h
#5 564.4 
#5 564.4   The item was not found.

通过apt-get安装Boost时,此标头低于/usr/include/python3.6m/Python.h.

推荐答案

Ubuntu Bionic太旧了,不能直接在https://packages.ubuntu.com上搜索并确认,但我认为你需要安装libpython3.6-dev,或者类似的东西(例如libpython3-dev,libpython-dev,python3.6-dev,python3-dev).我的假设是,python-dev要么是Python2,要么是libpython-devPython.h,但python-dev没有.无论是哪一个,libboost-all-dev都可能会将正确的包作为依赖项引入,这就是为什么不安装它就意味着CMake找不到Python.h的原因.

Python相关问答推荐

将从Python接收的原始字节图像数据转换为C++ Qt QIcon以显示在QStandardProject中

从 struct 类型创建MultiPolygon对象,并使用Polars列出[list[f64]列

Python主进程和分支进程如何共享gc信息?

强制venv在bin而不是收件箱文件夹中创建虚拟环境

在Python中对分层父/子列表进行排序

使用FASTCGI在IIS上运行Django频道

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

log 1 p numpy的意外行为

梯度下降:简化要素集的运行时间比原始要素集长

在matplotlib中删除子图之间的间隙_mosaic

将scipy. sparse矩阵直接保存为常规txt文件

为什么if2/if3会提供两种不同的输出?

如何使用OpenGL使球体遵循Python中的八样路径?

ModuleNotFoundError:没有模块名为x时try 运行我的代码''

提高算法效率的策略?

使用__json__的 pyramid 在客户端返回意外格式

pandas fill和bfill基于另一列中的条件

如何在Python 3.9.6和MacOS Sonoma 14.3.1下安装Pyregion

如何从比较函数生成ngroup?

如何提高Pandas DataFrame中随机列 Select 和分配的效率?