How can I create a batch file that compiles and executes a C++ program using cl ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sasads
Posts: 1
Joined: 08 Aug 2016 06:40

How can I create a batch file that compiles and executes a C++ program using cl ?

#1 Post by sasads » 08 Aug 2016 06:45

I want to compile and execute a c++ program from Python.

I need to:

1.open VS2015 x64 ARM Cross Tools Command Prompt
2.run

Code: Select all

%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" amd64

3.

Code: Select all

cl main.cpp

4.

Code: Select all

main.exe

I tried:

I thought I could create a batch file and execute it from Python. But I'm new to batch so I'm having trouble writing the batch file. Could you please help ?

Thanks

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How can I create a batch file that compiles and executes a C++ program using cl ?

#2 Post by aGerman » 08 Aug 2016 14:25

I never compiled a program with a batch code.
Try

Code: Select all

@echo off
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64
cl /EHsc "C:\somewhere\main.cpp"
call "C:\somewhere\main.exe"

Regards
aGerman

SirJosh3917
Posts: 36
Joined: 02 May 2016 18:59

Re: How can I create a batch file that compiles and executes a C++ program using cl ?

#3 Post by SirJosh3917 » 18 Aug 2016 21:08

Do you have to use one of the visual studio tools?
Personally I just use G++.

Code: Select all

@echo off
:main
cd %dir%
set dir=%~dp1
set name=%~n1
del "%dir%\%name%.exe"
cls
C:\MinGW\bin\g++ "%~1" -o "%dir%\%name%.exe" -static
if exist "%name%.exe" goto :program
pause
goto :main
:program
call "%name%.exe"
pause

Drag & Drop code.cpp, this will automatically:
1) Delete an existing "code.exe"
2) Compile the file dragged ontop of it (code.cpp) as "code.exe"
3) If compiling failed, it'll pause, and attempt to recompile.
4) When compiling succeeded, it'll run the code and pause at the end.

But if you need to get cl, you can use this - it doesnt run vcvarsall however, it just goes straight to the directory.

Code: Select all

::Minimize output from running the commands (compiler errors will not be silenced)
@echo off
::Temporarily change the directory to here
pushd "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64"
::Use cl to compile main.cpp
cl main.cpp
::Run main.exe
main.exe
::Quit the temporary changes.
popd

Post Reply