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

huangapple go评论77阅读模式
英文:

How to compile C++ code or Cython in a MS Windows Docker image

问题

I want to build a docker MS Windows image to compile python and cython code via command line. cython requires Microsoft Visual C++ 14.0 or greater (Link).

Example

The initial goal is to compile the example taken from cython.org within a Windows docker image.

hello.pyx

print('Hello World')

setup.py

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

When executing the following command in a bash shell (linux) it compiles.

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

MS Windows Docker image

I tried a variety of commands which I could find for installing Microsoft Visual C++, but nothing seemed to work. The following Dockerfile is my current default:

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" ]

You can create a Docker image windows:python3.11 via the following command (on a Windows machine):

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

Request

With this MS Windows Docker image in mind I try to compile the example and get the following result:

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/

Question: How to correctly set up Microsoft Visual C++ 14.0 (docker) for cython?

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/

Regarding the MS C++ compiler, I have found not one method to access the Microsoft C compiler cl.exe. Any help would be appreciated. Commandline only.

英文:

I want to build a docker MS Windows image to compile python and cython code via command line. cython requires Microsoft Visual C++ 14.0 or greater (Link).

Example

The initial goal is to compile the example taken from cython.org within a Windows docker image.

hello.pyx

print('Hello World')

setup.py

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

When executing the following command in a bash shell (linux) it compiles.

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

MS Windows Docker image

I tried a variety of commands which I could find for installing Microsoft Visual C++, but nothing seemed to work. The following Dockerfile is my current default:

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" ]

You can create a Docker image windows:python3.11 via the following command (on a Windows machine):

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

Request

With this MS Windows Docker image in mind I try to compile the example and get the following result:

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/

Question: How to correctly set up Microsoft Visual C++ 14.0 (docker) for cython?

> 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/

Regarding the MS C++ compiler, I have found not one method to access the Microsoft C compiler cl.exe. Any help would be appreciated. Commandline only.

答案1

得分: 0

以下是您提供的Dockerfile的中文翻译:

# 用于在MS Windows容器中编译cython的工作Dockerfile

# 当您在容器中通过'buildtools.exe'安装Visual Studio时,cython会找到C++编译器,但是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"]

#================================#
# 下载和构建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'))"

# 在powershell中导入chocolatey函数
RUN powershell "Import-Module ${env:ChocolateyInstall}\\helpers\\chocolateyProfile.psm1"

#================================#
# 安装python和cython
RUN powershell "choco install python -y"
RUN powershell "python -m pip install --upgrade pip build"
RUN powershell "pip install cython"

#================================#
# 添加Visual Studio + 推荐工具
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"]

希望这对您有所帮助。如果您需要更多的翻译或有其他问题,请随时告诉我。

英文:

Working Dockerfile for compiling cython in a MS Windows container

When you install Visual Studio via buildtools.exe in the container, the c++ compiler is found by cython, but still, the MS windows c compiler can not simply be called via the Windows shell. I guess that you have to add the compiler location to the PATH variable.

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" ]

huangapple
  • 本文由 发表于 2023年6月26日 22:02:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定