Build Tools for Visual Studio 2022 で Python3のC拡張をビルドする
モチベーション
前提
Windows 11 (64bit)
Python 3.10 (amd64)
バージョン整合性を確認
メジャーバージョンは2015年以降全部14なんだなー
Microsoft Visual C++ 14.x with Visual Studio 2022 (x86, x64, ARM, ARM64)
Microsoft Visual C++ 14.2 standalone: Build Tools for Visual Studio 2019 (x86, x64, ARM, ARM64)
Microsoft Visual C++ 14.2 with Visual Studio 2019 (x86, x64, ARM, ARM64)
Microsoft Visual C++ 14.1 standalone: Build Tools for Visual Studio 2017 (x86, x64, ARM, ARM64)
Microsoft Visual C++ 14.1 with Visual Studio 2017 (x86, x64, ARM, ARM64)
Microsoft Visual C++ 14.0 standalone: Visual C++ Build Tools 2015 (x86, x64, ARM)
Microsoft Visual C++ 14.0 with Visual Studio 2015 (x86, x64, ARM)
14.x (2023/03/04時点では14.3)でも大丈夫そうなので、2022の14.3 で進める 手順
https://scrapbox.io/files/6402af69bf2f41001db9a37b.png
微妙に見つけづらいところに隠れている
検索ボックスに Build Tools と入力すると見つけやすい
https://scrapbox.io/files/6402afe734b6fd001c857d6d.png
https://scrapbox.io/files/6402affbc12afb001c3f8b51.png
https://scrapbox.io/files/6402b04cc12afb001c3f8dac.png
Install Microsoft Visual Studio 2022 (or later).
入れます
Install the Python development workload and the optional Python native development tools option.
パス(Python自体のビルドをするわけじゃないので)
Install the latest Windows SDK (under Native development in the installer).
入れます
Optional: Set $env:PlatformToolset to your toolset version before building, if it doesn't detect it.
パス
Update to the latest setuptools Python package version.
パス
Wikiに従ってインストール
インストールを個別選択
MSVC v143 - VS 2022 C++ x64/x86 ビルドツール(最新) (最新、じゃない方を入れたら vcvarsall.bat が無かった)
Windows 11 SDK (10.0.22000.0)
https://scrapbox.io/files/6403cb4dc26182001c2dba48.png
構成ファイル
code:.vsconfig
{
"version": "1.0",
"components": [
"Microsoft.VisualStudio.Component.Roslyn.Compiler",
"Microsoft.Component.MSBuild",
"Microsoft.VisualStudio.Component.CoreBuildTools",
"Microsoft.VisualStudio.Workload.MSBuildTools",
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
"Microsoft.VisualStudio.Component.Windows11SDK.22000",
"Microsoft.VisualStudio.Component.VC.14.35.17.5.ATL.Spectre",
"Microsoft.VisualStudio.Component.VC.14.35.17.5.MFC.Spectre"
]
}
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build を見る
vcvarsall.bat がインストールされた!PythonのC拡張をビルドするにはこれが必須。
https://scrapbox.io/files/6403032e0f18b2001b2d4bb9.png
setup.py build は動いたけど、リンクでエラー!
https://scrapbox.io/files/640303914ee685001c83226a.png
code:console
C:\Users\taka\Project\python3\epp4>python3.10 setup.py build
running build
running build_ext
building 'fibonacci' extension
creating build
creating build\temp.win32-cpython-310
creating build\temp.win32-cpython-310\Release
"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\bin\HostX86\x86\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\include" "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\Include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\cppwinrt" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\cppwinrt" /Tcfibonacci.c /Fobuild\temp.win32-cpython-310\Release\fibonacci.obj
fibonacci.c
creating C:\Users\taka\Project\python3\epp4\build\lib.win32-cpython-310
"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\bin\HostX86\x86\link.exe" /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO "/LIBPATH:C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\libs" "/LIBPATH:C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0" "/LIBPATH:C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\PCbuild\win32" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\lib\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\ucrt\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22000.0\\um\x86" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\lib\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\ucrt\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22000.0\\um\x86" /EXPORT:PyInit_fibonacci build\temp.win32-cpython-310\Release\fibonacci.obj /OUT:build\lib.win32-cpython-310\fibonacci.cp310-win_amd64.pyd /IMPLIB:build\temp.win32-cpython-310\Release\fibonacci.cp310-win_amd64.lib
ライブラリ build\temp.win32-cpython-310\Release\fibonacci.cp310-win_amd64.lib とオブジェクト build\temp.win32-cpython-310\Release\fibonacci.cp310-win_amd64.exp を作成中
fibonacci.obj : error LNK2001: 外部シンボル __imp___Py_BuildValue_SizeT は未解決です
fibonacci.obj : error LNK2001: 外部シンボル __imp__Py_Initialize は未解決です
fibonacci.obj : error LNK2001: 外部シンボル __imp___PyArg_ParseTuple_SizeT は未解決です
fibonacci.obj : error LNK2001: 外部シンボル __imp__PyModule_Create2 は未解決です
build\lib.win32-cpython-310\fibonacci.cp310-win_amd64.pyd : fatal error LNK1120: 4 件の未解決の外部参照
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.35.32215\\bin\\HostX86\\x86\\link.exe' failed with exit code 1120
コンパイルは問題なさそうだけど、リンクで落ちている
64bit版Pythonなのにx86 (32bit)をリンクしようとしてる?
python setup.py build --plat-name=win-amd64
https://scrapbox.io/files/6403bef6a3b6f0001c2847de.png
ビルドとおった!ちゃんとamd64をリンクしている
ドキュメント
64bit版Pythonでビルドしてるのに、自動判別してくれないのね
ビルドされたpydのあるディレクトリに移動してimportして実行
うごいたーー
https://scrapbox.io/files/64030c1624f787001c71b297.png
次からオプション指定で困らないように、オプションを保存しておく
code:console
C:\Users\taka\Project\python3\epp4>python3.10 setup.py saveopts build --plat-name=win-amd64
running saveopts
Writing setup.cfg
running build
running build_ext
C:\Users\taka\Project\python3\epp4>python3.10 setup.py saveopts bdist --plat-name=win-amd64
running saveopts
Writing setup.cfg
running bdist
running bdist_dumb
running build
running build_ext
code:setup.cfg
C:\Users\taka\Project\python3\epp4>type setup.cfg
plat_name = win-amd64
plat_name = win-amd64
python -m build でビルドする
code:console
C:\Users\taka\Project\python3\epp4>python3.10 -m build -n
* Getting build dependencies for sdist...
running egg_info
writing fibonacci.egg-info\PKG-INFO
writing dependency_links to fibonacci.egg-info\dependency_links.txt
writing top-level names to fibonacci.egg-info\top_level.txt
reading manifest file 'fibonacci.egg-info\SOURCES.txt'
writing manifest file 'fibonacci.egg-info\SOURCES.txt'
* Building sdist...
running sdist
running egg_info
writing fibonacci.egg-info\PKG-INFO
writing dependency_links to fibonacci.egg-info\dependency_links.txt
writing top-level names to fibonacci.egg-info\top_level.txt
reading manifest file 'fibonacci.egg-info\SOURCES.txt'
writing manifest file 'fibonacci.egg-info\SOURCES.txt'
warning: sdist: standard file not found: should have one of README, README.rst, README.txt, README.md
running check
creating fibonacci-0.0.0
creating fibonacci-0.0.0\fibonacci.egg-info
copying files to fibonacci-0.0.0...
copying fibonacci.c -> fibonacci-0.0.0
copying setup.cfg -> fibonacci-0.0.0
copying setup.py -> fibonacci-0.0.0
copying fibonacci.egg-info\PKG-INFO -> fibonacci-0.0.0\fibonacci.egg-info
copying fibonacci.egg-info\SOURCES.txt -> fibonacci-0.0.0\fibonacci.egg-info
copying fibonacci.egg-info\dependency_links.txt -> fibonacci-0.0.0\fibonacci.egg-info
copying fibonacci.egg-info\top_level.txt -> fibonacci-0.0.0\fibonacci.egg-info
Writing fibonacci-0.0.0\setup.cfg
Creating tar archive
removing 'fibonacci-0.0.0' (and everything under it)
* Building wheel from sdist
* Getting build dependencies for wheel...
running egg_info
writing fibonacci.egg-info\PKG-INFO
writing dependency_links to fibonacci.egg-info\dependency_links.txt
writing top-level names to fibonacci.egg-info\top_level.txt
reading manifest file 'fibonacci.egg-info\SOURCES.txt'
writing manifest file 'fibonacci.egg-info\SOURCES.txt'
* Building wheel...
running bdist_wheel
running build
running build_ext
building 'fibonacci' extension
creating build
creating build\temp.win-amd64-cpython-310
creating build\temp.win-amd64-cpython-310\Release
"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\include" "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\Include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\cppwinrt" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\cppwinrt" /Tcfibonacci.c /Fobuild\temp.win-amd64-cpython-310\Release\fibonacci.obj
fibonacci.c
creating C:\Users\taka\AppData\Local\Temp\build-via-sdist-i036n7t5\fibonacci-0.0.0\build\lib.win-amd64-cpython-310
"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\bin\HostX86\x64\link.exe" /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO "/LIBPATH:C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\libs" "/LIBPATH:C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0" "/LIBPATH:C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\PCbuild\amd64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22000.0\\um\x64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\lib\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\ucrt\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22000.0\\um\x86" /EXPORT:PyInit_fibonacci build\temp.win-amd64-cpython-310\Release\fibonacci.obj /OUT:build\lib.win-amd64-cpython-310\fibonacci.cp310-win_amd64.pyd /IMPLIB:build\temp.win-amd64-cpython-310\Release\fibonacci.cp310-win_amd64.lib
ライブラリ build\temp.win-amd64-cpython-310\Release\fibonacci.cp310-win_amd64.lib とオブジェクト build\temp.win-amd64-cpython-310\Release\fibonacci.cp310-win_amd64.exp を作成中
コード生成しています。
コード生成が終了しました。
installing to build\bdist.win-amd64\wheel
running install
running install_lib
creating build\bdist.win-amd64
creating build\bdist.win-amd64\wheel
copying build\lib.win-amd64-cpython-310\fibonacci.cp310-win_amd64.pyd -> build\bdist.win-amd64\wheel\.
running install_egg_info
running egg_info
writing fibonacci.egg-info\PKG-INFO
writing dependency_links to fibonacci.egg-info\dependency_links.txt
writing top-level names to fibonacci.egg-info\top_level.txt
reading manifest file 'fibonacci.egg-info\SOURCES.txt'
writing manifest file 'fibonacci.egg-info\SOURCES.txt'
Copying fibonacci.egg-info to build\bdist.win-amd64\wheel\.\fibonacci-0.0.0-py3.10.egg-info
running install_scripts
C:\Users\taka\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\wheel\bdist_wheel.py:83: RuntimeWarning: Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect
if get_flag("Py_DEBUG", hasattr(sys, "gettotalrefcount"), warn=(impl == "cp")):
creating build\bdist.win-amd64\wheel\fibonacci-0.0.0.dist-info\WHEEL
creating 'C:\Users\taka\Project\python3\epp4\dist\.tmp-o0oxuiqh\fibonacci-0.0.0-cp310-cp310-win_amd64.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'fibonacci.cp310-win_amd64.pyd'
adding 'fibonacci-0.0.0.dist-info/METADATA'
adding 'fibonacci-0.0.0.dist-info/WHEEL'
adding 'fibonacci-0.0.0.dist-info/top_level.txt'
adding 'fibonacci-0.0.0.dist-info/RECORD'
removing build\bdist.win-amd64\wheel
Successfully built fibonacci-0.0.0.tar.gz and fibonacci-0.0.0-cp310-cp310-win_amd64.whl
できたー
参考