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
Preliminary Notes:
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 detailsI 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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With