I do not like compiling in visual studio’s IDE. I like compiling C++ with a batch file instead. 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 hobbies and work keep me on windows most of the time. So this is how I compile C++ with a batch file.
So, over the last few years I been slowing building and changing my script for compiling and here is what I am using right now.
Update: Here is my batch code for 2018
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.
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:
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 in to 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
So 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 for debugging. "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.com" 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.
And that is compiling C++ with a batch file. 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.