如何在 MS Windows Docker 映像中编译 C++ 代码或 Cython

问题描述 投票:0回答:1

我想构建一个 docker MS Windows 映像来通过命令行编译 python 和 cython 代码。

cython
需要 Microsoft Visual C++ 14.0 或更高版本(链接)。

示例

最初的目标是在 Windows docker 映像中编译取自 cython.org 的示例。

hello.pyx

print('Hello World')

setup.py

from setuptools import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("hello.pyx"))

在 bash shell (linux) 中执行以下命令时,它会编译。

$ python setup.py build_ext --inplace`
Compiling hello.pyx because it changed.
[1/1] Cythonizing hello.pyx

MS Windows Docker 镜像

我尝试了各种可以找到的用于安装 Microsoft Visual C++ 的命令,但似乎没有任何效果。以下

Dockerfile
是我当前的默认值:

Dockerfile

FROM mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-ltsc2019

SHELL ["cmd", "/S", "/C"]

# Download and build chocolatey
RUN powershell Set-ExecutionPolicy Bypass -Scope Process -Force
RUN powershell [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
RUN powershell iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

RUN powershell choco upgrade chocolatey

# Installing packages via chocolatey
RUN powershell choco install visualstudio2022buildtools -y --package-parameters "--includeRecommended"
RUN powershell choco install python -y

# Add bash shell
RUN powershell choco install git -y
RUN setx path "C:\\Program Files\\Git\\bin;%path%"

# adds gcc and gfortran
RUN powershell choco install mingw -y

ENTRYPOINT [ "cmd" ]

您可以通过以下命令创建 Docker 镜像

windows:python3.11
(在 Windows 机器上):

$ docker build --rm --file ./Dockerfile --tag windows:python3.11 .

请求

考虑到这个 MS Windows Docker 映像,我尝试编译示例并得到以下结果:

Microsoft Windows [Version 10.0.17763.4499]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\>pip install cython
Collecting cython
  Downloading Cython-0.29.35-py2.py3-none-any.whl (988 kB)
     ---------------------------------------- 988.4/988.4 kB 12.5 MB/s eta 0:00:00
Installing collected packages: cython
Successfully installed cython-0.29.35

C:\>python setup.py build_ext --inplace
running build_ext
building 'hello' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

问题:如何为 cython 正确设置 Microsoft Visual C++ 14.0 (docker)?

error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

关于MS C++编译器,我没有找到一种访问Microsoft C编译器的方法

cl.exe
。任何帮助,将不胜感激。仅限命令行。

python docker visual-c++ cython cl.exe
1个回答
0
投票

工作
Dockerfile
在 MS Windows 容器中编译 cython

当您通过容器中的

buildtools.exe
安装Visual Studio时,C++编译器会被cython找到,但是MS windows c编译器仍然不能简单地通过Windows shell调用。我想您必须将编译器位置添加到
PATH
变量中。

FROM mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-ltsc2019
ENV LANG=C.UTF-8

SHELL ["cmd", "/S", "/C"]

#================================#
# Download and build chocolatey
RUN powershell "Set-ExecutionPolicy Bypass -Scope Process -Force"
RUN powershell "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072"
RUN powershell "iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))"

# importing chocolatey functions in the powershell
RUN powershell "Import-Module ${env:ChocolateyInstall}\\helpers\\chocolateyProfile.psm1"

#================================#
# Installing python and cython
RUN powershell "choco install python -y"
RUN powershell "python -m pip install --upgrade pip build"
RUN powershell "pip install cython"

#================================#
# Adds Visual Studio + recommend tools
RUN powershell "Invoke-WebRequest -Outfile buildtools.exe -Uri https://aka.ms/vs/17/release/vs_BuildTools.exe"
RUN powershell ".\buildtools.exe --quiet --wait --norestart --nocache --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"

#================================#
ENTRYPOINT [ "cmd" ]
© www.soinside.com 2019 - 2024. All rights reserved.