Click here to Skip to main content
14,932,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What lines of code should I use when creating a .bat file that will automatically compile a c++ source file using command prompt and auto run the .exe ? assuming I am using visual c++ build tools and assuming the .bat file has the paths to the source. (on Windows)

Please give example specific to Visual c++ build tools compiler.

What I have tried:

code inside the batch file:
cd c:\<path-to-directory>
cl /EHsc program.cpp
    program.exe
Posted
Updated 5-Apr-20 19:15pm

You have to set the path and environment variables for command line builds first. See Build C/C++ Code on the Command Line | Microsoft Docs[^].

If you have created a VS project you can build it with the msbuild command line utility. See MSBuild Command-Line Reference - Visual Studio | Microsoft Docs[^].

Untested example:
SET VS=C:\Program Files (x86)\Microsoft Visual Studio 14.0
SET PROJ=path_to_your_project
CALL "%VS%\Common7\Tools\vcvars32.bat"
cl.exe /EHsc "%PROJ%\program.cpp"
IF NOT %ERRORLEVEL%==0 GOTO Done
"%PROJ%\program.exe"
:Done
   
Comments
Member 13969713 2-Sep-18 1:19am
   
Thanks but I need an example for "visual c++ build tools"(no visual studio installed) or does this example also apply anyway?
Jochen Arndt 2-Sep-18 5:09am
   
Should be similar with the build tools. The build tools are nothing else than the platform toolset(s) installed with VS. So the vcvars.bat files should be somewhere installed.

Check also your start menu. As far as I know the installer will create shortcuts to "open developer command prompt windows by using these batch files, so all the required environment variables are set and ready to use".
   
I've gotten away with a simpler method: (Uses MinGW) (Must be in environment variables) (if you go in cmd and type g++, it should say 'fatal error')

Use:

@ECHO OFF
g++ Filename.cpp -o SomeName.exe


And to run it..
Use:
SomeName.exe

for c, use gcc instead.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900