3

我正在使用 makefiles (GNU Make) 在命令行上使用 Visual C++ Express 进行编程。为此,我必须调用 Visual Studio 批处理文件vsvars32.bat来设置环境。每次我在使用 make 之前打开一个新的 cmd.exe 时都必须这样做。当我尝试从我的makefile调用批处理文件时,它显然将批处理文件作为自己的进程执行,因为之后的环境是相同的。

所以我的问题是:有没有办法像Linux/Unix bash的内置源命令一样在 cmd.exe 中执行脚本?当然,除了在 Windows 上安装 bash。

发布我自己的答案后编辑:

上面的问题不太对,应该是这样的:

是否可以从生成文件中调用更改环境的批处理文件,以便更改的环境对于生成文件中调用的其他程序仍然存在?

原问题的答案是肯定的:可以使用cmd.exe的内置调用命令。但是由于call是一个内置命令而不是一个真正的程序,它在 makefile 中不起作用,只有当你从另一个批处理文件中调用一个批处理文件时。

4

6 回答 6

4

Answer compiled from the previous answers:

I made a batch file called make.bat which contains the following:

call "%VS90COMNTOOLS%vsvars32.bat"
call 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.

Edit: After overflowing my PATH variable by repeatedly calling vsvars32.bat, I made the following changes:

if not "%VISUALCVARS%" == "TRUE" (
set VISUALCVARS=TRUE
call "%VS90COMNTOOLS%vsvars32.bat"
)
call make.exe %*

于 2009-01-16T15:17:41.693 回答
1

使用“呼叫”:

@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

这是我们用来构建 linq 提供程序的 cmd 文件。

于 2009-01-16T11:39:06.677 回答
1

至少在我安装的 Visual Studio(尽管有点古老的 VS .NET 2003)中,VS 开始菜单组中的链接之一是打开一个已经设置好环境的 cmd.exe 实例。您可能会发现这些很有帮助:

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.

于 2009-01-16T12:36:18.603 回答
1

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)

%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.

于 2009-01-16T12:53:31.547 回答
0

Wrap GNU make in a script (mmake.bat). Put the script in the path somewhere.

The script itself should run the vsvars32.bat and then make, like this

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).

于 2009-01-16T14:11:15.510 回答
0

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).

2) Write a wrapper batch file that calls the environment setup script then calls the 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).

于 2009-06-23T17:52:37.943 回答