Compiling C++ with a batch file

Not only am I a minimalist when it comes to software but I also like to have total control over what my software is doing. This is why I do not like Visual Studio’s IDE. It is a bloated mess that I do not want to fight with. That is why I compile with the command line options. More specifically I like compiling C++ with a batch file.

This used to be a lot easier. Or at least it felt like it was 15 years ago. But Microsoft seems to make this harder to do with each updated version of visual studio. If you’re on Linux this may not be a problem for you. But my work keeps me on windows most of the time.

So over the last few years, I have been slowly building and changing my script for compiling. This is the script I use to compile C++ right now.

compiling c++ with a batch file

Update: Here is my batch code for 2018 and 2019

At some point Windows 10 started getting weird about the pushd command so I had to reorder the code. I also added support for Visual Studio 2017.

For 2019 you just change the year in the path to match the new install path.

compiling c++ with a batch file

Line by line explanation

The first thing I do is store all the compiler flags I want to use in a batch file variable.

set CommonCompilerFlags=-Zi /Wv:18

Then I do the same thing with linker flags.

set CommonLinkerFlags=user32.lib gdi32.lib opengl32.lib

Then because I don’t want to have to worry about setting paths on every computer I work on I just run the file that comes with visual studio that sets the path for this session. This location will most likely change as new versions of visual studio come out.

For Visual Studio 2017: (For 2019 just change the year in the path.)

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64

For Visual Studio14:

call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64

Now I like my compiled files to be saved neatly into a build folder so here I make the folder. The %~dp is just a shortcut for the current directory the script is running in so you do not have to hardcode it.

mkdir %~dp0build

This will change the current directory the script is working into the new build folder.

pushd %~dp0build

Then I move any assets that the program needs to run. In this case I have shader files. So I use robocopy to put them in a Shader folder inside my new build folder.

robocopy ..\Shaders Shaders /MIR

I also move some other files into a data folder inside the build folder.
robocopy

 ..\data data /MIR

Now we run the compiler. We add our compiler and linker flags and the new compiled file should show up in the build directory.

cl %CommonCompilerFlags% "..\OpenGL.cpp" %CommonLinkerFlags%'

Reset and go back to the original current directory.

popd

Sometimes I add the pause and sometimes I don’t. It depends on how much debugging I am doing.

pause

So, the full build.bat script looks something like this and I have it saved in with my source files.

@echo off
REM Notes: I had to add "/Wv:18" to compile in new Visual Studio
set CommonCompilerFlags=-Zi /Wv:18
set CommonLinkerFlags=user32.lib gdi32.lib opengl32.lib
REM if the cl command is not working we will need to set up the path by running vcvarsall.bat
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
REM If 2017 is not found try the old location
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
mkdir "%~dp0build"
pushd "%~dp0build"
robocopy ..\Shaders Shaders /MIR
robocopy ..\data data /MIR
cl %CommonCompilerFlags% "..\OpenGL.cpp" %CommonLinkerFlags%
popd
pause

Debug with Visual Studio

Like it or not Visual Studio is one of the better debuggers. But how do you debug with Visual Studio if you are compiling with a script? That is easy. I have another script call debug.bat and it looks like this.

REM Open program in Visual Studio 2017 for debugging.
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.com" build\OpenGL.exe openGL.cpp

or for the newer Visual Studio

REM Open program in Visual Studio 2019 for debugging
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe" build\OpenGL.exe openGL.cpp

Just point it to where the exe file was saved. In this case, it was saved in the build directory. Then you can add cpp files on for it to open automatically when the debugger opens.

Now your ready to compile and debug!

That is all there is to it! I remember when I first started to learn C++ compiling seem like such a pain.

Now this is how I compile C++ for all my projects and debug all my code. If you wanted to get fancy you could also add some macros or shortcuts to the scripts so that it is all seamless.

I hope you enjoy this method as much as I have!


Subscribe and discover even more!

I hope you enjoyed this article! I am always posting new and helpful things! If you like what you see then I would love to be able to send you more! Please subscribe to my newsletter for the latest information and exclusive content!

* indicates required

Leave a Reply

Your email address will not be published. Required fields are marked *