简体   繁体   English

从python脚本调用CMake导致“无法创建命名生成器”

[英]Call CMake from python script results in “Could not create named generator”

I'm building a project that should be able to move freely between machines. 我正在构建一个能够在机器之间自由移动的项目。 In order to not have hundreds of MB of libs I'm writing a series of python scripts that download and build the dependencies as well as my own project. 为了没有数百MB的lib,我正在编写一系列python脚本来下载和构建依赖项以及我自己的项目。 I'm using CMake to generate VS projects. 我正在使用CMake生成VS项目。

To call CMake I build a command line and I use python subprocess.check_call as below 要调用CMake我构建一个命令行,我使用python subprocess.check_call ,如下所示

cmakeCmd = ["cmake.exe", '-G "Visual Studio 11 Win64"', build_path]
retCode = subprocess.check_call(cmakeCmd, stderr=subprocess.STDOUT, shell=True)

The problem is that if I use -G option from CMake I get the following error regardless of the generator I choose: 问题是,如果我使用CMake中的-G选项,无论选择哪个生成器,我都会收到以下错误:

CMake Error: Could not create named generator  "Visual Studio 11 Win64"

I was thinking that should be some enviroment variable missing but the python path is complete with all my systems' variables. 我认为应该是一些环境变量丢失,但python路径已完成我所有系统的变量。

What is odd is that if I don't set the generator and let CMake choose the default one the script runs fine. 奇怪的是,如果我没有设置生成器并让CMake选择默认生成器,则脚本运行正常。 I have no ideia why. 我没有想到的原因。

You need to split the -G flag from its value in the command. 您需要在命令中将-G标志与其值分开。 Python appears to be effectively invoking Python似乎在有效地调用

cmake "-G Visual Studio 11 Win64" <build_path>

What you want is more like: 你想要的更像是:

cmakeCmd = ["cmake.exe", "-G", "Visual Studio 11 Win64", build_path]

Should be: 应该:

cmakeCmd = ['cmake', '-G', 'Visual Studio 11 Win64', build_path]

For the sake of cross-platformness , I recommend to do the following: 为了跨平台 ,我建议执行以下操作:

if sys.platform.startswith('win'):
    shell = True
else:
    shell = False

retCode = subprocess.check_call(cmakeCmd, stderr=subprocess.STDOUT, shell=shell)

if you plan to work with the project on several platforms (including Unix OS family). 如果您打算在多个平台(包括Unix OS系列)上使用该项目。 By the way, since you are using shell=True on Windows, you can omit .exe suffix in the cmakeCmd (which is good if this script is used on Unix OS family too as there is no such thing as .exe ). 顺便说一下,由于你在Windows上使用shell=True ,你可以在cmakeCmd省略.exe后缀(如果这个脚本在Unix OS系列上使用也很好,因为没有.exe这样的东西)。

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

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