windows 使用 make 调用批处理文件并使更改持久化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/450105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling batch files with make and making changes persistent
提问by Xenu
I'm programming with Visual C++ Express on the command line using makefiles (GNU Make). For this to work, I have to call the Visual Studio batch file vsvars32.batto set up the environment. This has to be done everytime I open a new cmd.exe, before using make. When I try to call the batch file from my makefile, it obviously executes the batch file as an own process, because the environment is the same afterwards.
我正在使用 makefiles (GNU Make) 在命令行上使用 Visual C++ Express 进行编程。为此,我必须调用 Visual Studio 批处理文件vsvars32.bat来设置环境。在使用 make 之前,每次我打开一个新的 cmd.exe 时都必须这样做。当我尝试从我的 makefile 调用批处理文件时,它显然将批处理文件作为自己的进程执行,因为之后的环境是相同的。
So my question: is there a way to execute scripts in cmd.exe like the built-in sourcecommand of the Linux/Unix bash? Apart from installing bash on Windows, of course.
所以我的问题是:有没有办法像Linux/Unix bash的内置源命令一样在 cmd.exe 中执行脚本?当然,除了在 Windows 上安装 bash 之外。
Edit after posting my own answer:
发布我自己的答案后编辑:
The above question is not quite right, it should be like this:
上面的问题不太对,应该是这样的:
Is it possible to call an environment-changing batch file from within a makefile, so that the changed environment persists for the other programs called in the makefile?
是否可以从 makefile 中调用改变环境的批处理文件,以便对 makefile 中调用的其他程序保留更改的环境?
The answer to the original question is yes: you can use the built-in callcommand of cmd.exe. But since callis a built-in command and not a real program, it doesn't work in a makefile, only if you call a batch file from another batch file.
原问题的答案是肯定的:可以使用cmd.exe内置的调用命令。但是由于call是一个内置命令而不是一个真正的程序,它在 makefile 中不起作用,只有当您从另一个批处理文件调用批处理文件时。
回答by Xenu
Answer compiled from the previous answers:
从以前的答案编译的答案:
I made a batch file called make.bat which contains the following:
我制作了一个名为 make.bat 的批处理文件,其中包含以下内容:
call "%VS90COMNTOOLS%vsvars32.bat"
call make.exe %*
调用“%VS90COMNTOOLS%vsvars32.bat”
调用make.exe %*
This does the job. But calling an environment-changing batch file from within a makefile, so that the changed environment persists for the other programs called in the makefile, seems to be impossible.
这可以完成工作。但是,从 makefile 中调用改变环境的批处理文件,以便对 makefile 中调用的其他程序保持更改的环境,这似乎是不可能的。
Edit:After overflowing my PATH variable by repeatedly calling vsvars32.bat, I made the following changes:
编辑:通过重复调用 vsvars32.bat 溢出我的 PATH 变量后,我进行了以下更改:
if not "%VISUALCVARS%" == "TRUE" (
set VISUALCVARS=TRUE
call "%VS90COMNTOOLS%vsvars32.bat"
)
call make.exe %*
如果不是 "%VISUALCVARS%" == "TRUE"(
设置 VISUALCVARS=TRUE
调用 "%VS90COMNTOOLS%vsvars32.bat"
)
调用 make.exe %*
回答by Frans Bouma
use 'Call':
使用“呼叫”:
@echo off
pushd.
call "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars3235.bat"
msbuild LinqSupportClassesSDKBuild.csproj /t:rebuild /p:Configuration=Release /nologo /v:q /clp:ErrorsOnly;
popd
this is the cmd file we use to build our linq provider.
这是我们用来构建 linq 提供程序的 cmd 文件。
回答by Jay
At least in my install of Visual Studio (albeit somewhat ancient VS .NET 2003), one of the links in the VS start menu group is to open a cmd.exe instance with the environment already setup. You might find these helpful:
至少在我安装的 Visual Studio(虽然有些古老的 VS .NET 2003)中,VS 开始菜单组中的链接之一是打开一个 cmd.exe 实例并且环境已经设置。您可能会发现这些有用:
- How to Add Visual Studio Command Prompt (VSCP) to your IDE as a tool?
- Running the command prompt from visual studio tools menu
- Shortcut: Launch Visual Studio Command Prompt from Visual Studio
- 如何将 Visual Studio 命令提示符 (VSCP) 作为工具添加到您的 IDE?
- 从 Visual Studio 工具菜单运行命令提示符
- 快捷方式:从 Visual Studio 启动 Visual Studio 命令提示符
They are more geared toward launching the command prompt from the IDE, but they do include information on launching it with the appropriate environment as well which you may find helpful for your purposes.
它们更适合从 IDE 启动命令提示符,但它们确实包含有关在适当的环境中启动它的信息,您可能会发现这些信息对您的目的有所帮助。
回答by Chris Becke
How do you launch your console? If you are just launching 'cmd' then instead, create a shortcut that executes (%comspec% resolves to c:\windows\cmd.exe or whatever is relevent on your system)
你如何启动你的控制台?如果您只是启动“cmd”,那么请创建一个执行的快捷方式(%comspec% 解析为 c:\windows\cmd.exe 或任何与您的系统相关的内容)
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat"" x86
Obviously, change the path to point to the proper installation folder. More generally, as the above poster pointed out, if a .cmd file needs to process another .cmd file rather than launch it as a seperate process, use the 'call' batch command.
显然,更改路径以指向正确的安装文件夹。更一般地说,正如上面的海报所指出的,如果一个 .cmd 文件需要处理另一个 .cmd 文件而不是将它作为单独的进程启动,请使用“调用”批处理命令。
回答by Chris Becke
Wrap GNU make in a script (mmake.bat). Put the script in the path somewhere.
将 GNU make 包装在脚本中 (mmake.bat)。将脚本放在路径中的某处。
The script itself should run the vsvars32.bat and then make, like this
脚本本身应该运行 vsvars32.bat 然后make,就像这样
vsvars32.bat
make %*
As far as I remember, invoking a script from another script like this is done within the same shell (similar to Bash "." command).
据我所知,像这样从另一个脚本调用脚本是在同一个 shell 中完成的(类似于 Bash "." 命令)。
回答by bta
I have found three solutions to this problem:
我找到了解决这个问题的三种方法:
1) If the environment variables being set by the batch file are static (that is, they are always the same values), set those values for your entire user profile. Right-click on My Computer, click Properties-->Advanced-->Environment Variables. Add the variables from the batch file to the User Variables or System Variables section (User variables are only visible by you, System variables are visible by all users of that computer).
1) 如果批处理文件设置的环境变量是静态的(即它们总是相同的值),请为整个用户配置文件设置这些值。右击我的电脑,点击属性-->高级-->环境变量。将批处理文件中的变量添加到用户变量或系统变量部分(用户变量仅对您可见,系统变量对该计算机的所有用户可见)。
2) Write a wrapper batch file that calls the environment setup script then calls the Makefile.
2) 编写一个包装批处理文件,调用环境设置脚本,然后调用 Makefile。
3) Instead of using the SET command to set environment variables in the batch file, use the SETX command (requires the Windows Resource Kit). SETX is similar to SET, except it makes its changes to the master environment in the registry and will take effect in all command prompts launched in the future (but not the current one).
3)不要使用SET命令在批处理文件中设置环境变量,而是使用SETX命令(需要Windows资源工具包)。SETX 与 SET 类似,不同之处在于它对注册表中的主环境进行更改,并将在未来启动的所有命令提示符中生效(但不是当前的)。