简体   繁体   English

在 Windows 上使用 cmake 构建 x86(32 位)项目时使用 64 位 python3 解释器

[英]Use 64 bit python3 interpreter when building x86 (32 bit) project with cmake on Windows

Background背景

I have mutiplatform C++ project.我有多平台 C++ 项目。 Python is used only to download some dependencies from strange places and generate some C++ code. Python 仅用于从陌生的地方下载一些依赖项并生成一些 C++ 代码。 Python is NOT linked to any target, so I do not care what kind of platform it uses. Python 没有链接到任何目标,所以我不在乎它使用什么样的平台。

Problem问题

When configuring project for Windows 64 bit platform, python is found( find_package ) without any problems.为 Windows 64 位平台配置项目时,发现 python ( find_package ) 没有任何问题。 On my machine only Python 3.10.0 for Win64 is installed.在我的机器上只安装了适用于 Win64 的 Python 3.10.0。

Now when I'm trying build same project for x86 (win32) it fails to configure since can't find 32 bit python.现在,当我尝试为 x86 (win32) 构建相同的项目时,由于找不到 32 位 python,因此无法配置。

Here is MCVE:这是MCVE:

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(findPythonToGenerate CXX)
find_package(Python3 REQUIRED)

configure_file(test.h.in test.h)

add_executable(foo main.cpp ${CMAKE_CURRENT_BINARY_DIR}/test.h)
target_include_directories(foo PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

main.cpp

#include <iostream>
#include "test.h"

int main()
{
    std::cout << PYTHON_EXE << '\n';
    return 0;
}

test.h.in

#pragma once

#define PYTHON_EXE "@Python3_EXECUTABLE@"

Now when building this for Windows 64 is just fine:现在为 Windows 64 构建它就可以了:

f:\mcve>cmake --version
cmake version 3.20.21032501-MSVC_2

CMake suite maintained and supported by Kitware (kitware.com/cmake).

f:\mcve>rmdir /Q /S build32 build64
The system cannot find the file specified.

f:\mcve>cmake -S . -B build64 -A x64 -Thost=x64 -G "Visual Studio 16 2019"
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19044.
-- The CXX compiler identification is MSVC 19.29.30145.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python3: C:/Python310/python.exe (found version "3.10.0") found components: Interpreter
-- Configuring done
-- Generating done
-- Build files have been written to: F:/mcve/build64

f:\mcve>cmake -S . -B build32 -A Win32 -Thost=x86 -G "Visual Studio 16 2019"
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19044.
-- The CXX compiler identification is MSVC 19.29.30145.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Python3 (missing: Python3_EXECUTABLE Interpreter)
Call Stack (most recent call first):
  C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.20/Modules/FindPython/Support.cmake:3165 (find_package_handle_standard_args)
  C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.20/Modules/FindPython3.cmake:485 (include)
  CMakeLists.txt:4 (find_package)


-- Configuring incomplete, errors occurred!
See also "F:/mcve/build32/CMakeFiles/CMakeOutput.log".

Question问题

Can I configure cmake in some way that it give me access to python interpreter ignoring what is the current platform I'm building for?我可以以某种方式配置cmake ,它可以让我访问 python 解释器,而忽略我正在构建的当前平台是什么? I do not want to install extra python - I do not need it.我不想安装额外的 python - 我不需要它。

I'm building 32 bit platform only to resolve build issues specific for that platform.我正在构建 32 位平台只是为了解决特定于该平台的构建问题。

Here is similar question , but it is a different flavor, since I do not link anything with python.这是类似的问题,但它是一种不同的风格,因为我没有将任何东西与 python 链接。

Side Note边注

I think it was OK with older version of cmake .我认为旧版本的cmake没问题。 Recently I've updated my Visual Studio 2019 and as a result cmake too.最近我更新了我的 Visual Studio 2019,因此也更新了cmake Sadly I can't tell it for sure.可悲的是,我不能肯定地说。

I just checked logs on build machine (which builds all platforms everyday) and on both platforms cmake 3.19 is used and same version of python and I'm sure it is 64 bit version.我刚刚检查了构建机器(每天构建所有平台)上的日志,并且在两个平台上都使用了cmake 3.19和相同版本的 python,我确定它是 64 位版本。 So looks like regression in cmake 3.20 , so I raised issue .所以看起来像cmake 3.20中的回归,所以我提出了问题

Ok turns out that problem was type of python installation.好的,原来问题是python安装的类型。

Some time ago I installed python 3.10 for "current user" instead for "local machine".前段时间我为“当前用户”安装了python 3.10,而不是为“本地机器”安装了python 3.10。 Apparently i didn't had to build for Win32 since that time and when I had to it happen just after MSVC upgrade.显然,从那时起我就不必为 Win32 构建了,而当我不得不在 MSVC 升级之后发生这种情况时。

So make sure you install python for "LOCAL MACHINE".因此,请确保为“本地机器”安装 python。

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

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