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
Update: Here is my batch code for 2018 and 2019
For 2019 you just change the year in the path to match the new
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.
This will change the current directory the script is working into the new build folder.
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.
..\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.
Sometimes I add the pause and sometimes I don’t. It depends on how much debugging I am doing.
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
Now your ready to compile and debug!
That is all there is to it. This is how I compile 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 found this helpful and if you have some tips to make this better please leave a comment below!
I would love to see what you come up with!