Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2017 command line tools don't work when launched from batch file

I am experiencing a very strange behavior, old batch file works well under windows XP. Why doesn't it work when compiling a simple file like hello.c under (Vs2017 + Win10) by a batch file in cmd window?

When realizing that Win10 has new security policy,,I read some articles on Microsoft's website. They recommend using the developer command-line window for command-line compilation.

Indeed, manual operation works well. But when I logged on Win10 as a super administrator and tried to run everything via a batch file, it didn't work, just finished the environment configuration.

When running the commands in the batch file manually, everything works as expected (executable file successfully generated). What is wrong with that?

Here is the content of the batch file:

%comspec% /k "C:\Program Files(x86)\Microsoft Visual Studio\2017  \Community\VC\Auxiliary\Build\vcvars64.bat"
cd g:\testdir
g: 
cl TestBatFileCompile.c
like image 371
user10716938 Avatar asked Nov 07 '22 23:11

user10716938


1 Answers

Preliminary Notes:

  • vcvars64.bat simply calls vcvarsall.bat and it passes the x64 argument to it, followed by its own (if any)
  • vcvarsall.bat mainly sets some env vars (set VSCMD_DEBUG=3 prior running it, for verbose output) required for the VStudio build tools to work. Check [MS.Docs]: Build C/C++ code on the command line for more details

I enhanced / simplified your example for more clarity:

script.bat:

@echo off

echo Running vcvars...
%comspec% /K "c:\Install\x86\Microsoft\Visual Studio Community\2017\VC\Auxiliary\Build\vcvarsall.bat" x64
echo Ran vcvars: %ERRORLEVEL%

echo Running cl...
cl /nologo dummy.c /link /NOLOGO
echo Ran cl: %ERRORLEVEL%

dummy.c:

int main() {
    return 0;
}

Output:

e:\Work\Dev\StackOverflow\q053523085>dir /b
dummy.c
script.bat

e:\Work\Dev\StackOverflow\q053523085>script.bat
Running vcvars...
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.2
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

e:\Work\Dev\StackOverflow\q053523085>rem HMMM, SOMETHING DOESN'T SEEM QUITE RIGHT. LET'S TRY EXITING CMD...

e:\Work\Dev\StackOverflow\q053523085>exit
Ran vcvars: 0
Running cl...
'cl' is not recognized as an internal or external command,
operable program or batch file.
Ran cl: 9009

What happened?

  • cmd /K ([MS.Docs]: Cmd) opened a new cmd instance, on top of the existing one (using the same window)
    • vcvarsall was called in this (2nd) instance, did its job and finished, and things seem to have been left in the air
  • But after typing exit, it turns out that cl did run, but in the 1stcmd instance (in the background, if you will), and since the variables were not previously set by vcvarsall, it failed

To make things work, invoke vcvarsall using [MS.Docs]: call:

call "c:\Install\x86\Microsoft\Visual Studio Community\2017\VC\Auxiliary\Build\vcvarsall.bat" x64

Output (in a new cmd window):

e:\Work\Dev\StackOverflow\q053523085>dir /b
dummy.c
script.bat

e:\Work\Dev\StackOverflow\q053523085>script.bat
Running vcvars...
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.2
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
Ran vcvars: 0
Running cl...
dummy.c
Ran cl: 0
e:\Work\Dev\StackOverflow\q053523085>dir /b
dummy.c
dummy.exe
dummy.obj
script.bat
like image 138
CristiFati Avatar answered Nov 15 '22 05:11

CristiFati