![](/img/trans.png)
[英]Visual Studio Code launched from command line conflates the two processes
[英]Visual Studio 2017 command line tools don't work when launched from batch file
我遇到了一個非常奇怪的行為,舊的批處理文件在Windows XP下運行良好。 在cmd窗口中通過批處理文件編譯(Vs2017 + Win10)下的hello.c
這樣的簡單文件時,為什么不能正常工作?
當意識到Win10有新的安全策略時,我在微軟的網站上閱讀了一些文章。 他們建議使用開發人員命令行窗口進行命令行編譯。
實際上,手動操作效果很好。 但是,當我以超級管理員身份登錄Win10並嘗試通過批處理文件運行所有內容時,它無法正常運行,只是完成了環境配置。
手動運行批處理文件中的命令時,一切都按預期工作(成功生成可執行文件)。 這有什么問題?
以下是批處理文件的內容:
%comspec% /k "C:\Program Files(x86)\Microsoft Visual Studio\2017 \Community\VC\Auxiliary\Build\vcvars64.bat"
cd g:\testdir
g:
cl TestBatFileCompile.c
初步說明 :
set VSCMD_DEBUG=3
,用於詳細輸出),以使VStudio構建工具工作。 檢查[MS.Docs]:在命令行上構建C / C ++代碼以獲取更多詳細信息 為了更清晰,我增強/簡化了您的示例:
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;
}
輸出 :
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
發生了什么?
cmd /K
( [MS.Docs]:Cmd )在現有的cmd實例之上打開一個新的cmd實例(使用相同的窗口)
要使工作正常, 請使用[MS.Docs]調用vcvarsall :call :
call "c:\Install\x86\Microsoft\Visual Studio Community\2017\VC\Auxiliary\Build\vcvarsall.bat" x64
輸出 (在新的cmd窗口中):
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.