Introduction
There are times when it is useful to compile and build a small sample program without needing to load an IDE and go through the process of setting up a new project. This tip shows a simple method of setting up a command process that can be used with minimal work.
Background
If you are not familiar with the use of the command processor in Windows, this tip is still quite easy to set up.
Using the Code
The idea for this came up as I was trying to reproduce problems that were posted on CodeProject. Rather than fire up Visual Studio or Eclipse, I just wanted to compile and run the user's source code in order to try and help diagnose their problem. The issue I faced was trying to remember the various paths to the different compilers, and the compiler options, in order to build the code. I got round this by creating simple command scripts that would set up the environment, which I could run in a command shell.
I first created separate subdirectories for each language that I was likely to use (C++ and Java first). I then created the following _setup.cmd scripts in each directory:
- C++ (Visual Studio creates the called script as part of its installation process.)
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\VC\Auxiliary\Build\vcvarsall.bat" x86 %*
- Java
@ECHO off
TITLE Java Development
set jpath=C:\Program Files\Java\jdk1.8.0_162\bin
PATH=%jpath%;%PATH%
set j
Obviously, these scripts need to be modified for your own environment, and directory paths.
So I now have some scripts which allow me to set up an environment to build either C++ or Java programs. But I still need to open a command shell and navigate to the correct directory in order to utilise them. How to make it even easier to get the shell open in the correct directory and ready to run with a simple mouse click? The answer is to create a Windows shortcut in the relevant directory with the following properties:
Target: (this is the same for all languages, unless you change the name of the _setup.cmd script)
C:\Windows\System32\cmd.exe /k .\_setup.cmd
Start in: (the name of the directory containing the _setup.cmd script)
C:\Users\myname\Documents\C++ ... or Java etc. as appropriate
If I now click the shortcut link, Windows will open a command shell in that directory and run the _setup.cmd script so it is ready to run a build.
The next challenge (for C++ especially) is to remember all the command line options for the compiler and linker. The easiest way is to create another script that will do the work for me. The obvious name for such a script is make.cmd (I was used to using make
in Unix/Linux in my professional life). But I also want to type just "make
" at the command line rather than "make sample.cpp
". Thankfully, the command shell makes this fairly easy.
The make
shell for C++ is:
@ECHO OFF
SET ff=%1
IF "%ff%" == "" SET ff=Test
SET cf=%ff:.cpp=%
@ECHO ON
cl /D "UNICODE" /EHsc /MDd /nologo /Od /Oy- /RTC1 /W3 %cf%.cpp
@ECHO OFF
IF ERRORLEVEL 1 GOTO :EOF
@ECHO ON
%cf%
The make
script for Java is more or less the same:
@ECHO OFF
SET ff=%1
IF "%ff%" == "" SET ff=Test
SET jf=%ff:.java=%
@ECHO ON
javac %jf%.java
@ECHO OFF
IF ERRORLEVEL 1 GOTO :EOF
@ECHO ON
java %jf%
In each case, the sequence of commands (ignoring the @ECHO
statements):
- Capture the file name (if any) on the command line
- If it is blank, then use a default source name (I use Test)
- Remove any extension from the name (.cpp or .java)
- Build the program, adjusting the options to your own needs
- If the compile or link failed, then skip to the end
- Otherwise, run the final executable
So, I can now navigate to either of my directories, click on the link and a command shell opens. I can then simply type "make
", "make Test
" or "make Test.cpp
" in the command shell to test the code. I also have versions for Android and PHP, but leave those as exercises for the reader.
History