Search code examples
cvisual-studiobatch-filecmdcompilation

The using of visual studio compiler by cmd.exe + batch file


Could you help me please with the using of visual studio compiler by cmd.exe + batch file?

I need to create batch file that will be executed in cmd.exe (C:\WINDOWS\system32\cmd.exe). The body of .bat-script is:

"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2017.lnk" 

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe" D:\temp\ctemplate\2\helloexample.c

Means I want to prepare command line with visual studio environment and then I want to compile file helloexample.c.

The shrtcut/link contains the target: %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"

During the execution I see that only first line is handled.

The body of c-script is:

#include <stdio.h>

int main()
{
    printf("Hello, World!");
    return 0;
}

Solution

  • During the execution I see that only first line is handled.

    If you want to run a batch file from another batch file you have to call it. Unlike an executable code run from a batch file, if you just run another batch file, the system does not return control to the calling batch file. That is why only first line is handled.

    So add call to the first line of your batch file:

    call "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2017.lnk" 
    
    "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe" D:\temp\ctemplate\2\helloexample.c