Thursday, October 5, 2017

Write a Batch File & Make File in Notepad

When writing C++ programs, you often want to set up your command prompt for building by setting the path and drive letter of your project.  Use a batch file to do this.

To compile all your files & run your program, you will accomplish this via a make file.  The batch file can call the make file.

Batch File

Sample batch file:

@echo off
cls
set DRIVE_LETTER=%CD:~0,2%
set PATH=%DRIVE_LETTER%\TDM-GCC-64\bin;%DRIVE_LETTER%\TDM-GCC-64\wxWidgets-3.1.0\lib\gcc510TDM_x64_dll;c:\windows\system32;c:\"Program Files"\KDiff3
set PROJECT_PATH=.
mingw32-make DRIVE_LETTER="%DRIVE_LETTER%" CURRENT_DIR="%PROJECT_PATH%"


For a complete tutorial, visit https://www.tutorialspoint.com/batch_script/batch_script_files.htm


  • Save batch files as .bat or .cmd at the end of the file name
  • @echo off
    • By default, a batch file will display its command as it runs.  
    • The purpose of the echo off command is to turn off the display for the whole script, except for the echo off command itself. 
    • The @ in front makes the command apply to itself as well
  • cls
    • clear the screen
  • set DRIVE_LETTER=%CD:~0,2%
    • Variables can be initialized via the 'set' command
    • if the value needs to be numeric in nature then add /A after the word set
    • DRIVE_LETTER is the variable name
    • %CD:~0,2% is the value that DRIVE_LETTER is being set to
    • %CD% prints the current working directory of the batch file or command running it
    • %CD:~0,3% will grab the first three characters of the path
    • %CD:~0,2% will grab the first two characters of the path
  • set PATH=something
    • PATH is the variable name
    • something is the path.  The example above sets the environment variable to the g++ compiler as well as wxWidgets
  • set PROJECT_PATH=.
    • PROJECT_PATH is the variable name
    • . is the current directory
  • mingw32-make DRIVE_LETTER="%DRIVE_LETTER%" CURRENT_DIR="%PROJECT_PATH%
    • mingw32-make will call the makefile that is in the specified directory
    • same as mingw32-make C: .

Make File

Use a make file to compile all your files & run your program without having to manually type everything in every time (saves you time).

Sample makefile:

# I am a comment.
#You can make variables in make files.  AutomatedMakefile is a variable
AutomatedMakefile = am

#the variable CC will be the compiler to use
CC = g++

#CXXFLAGS are the options I will pass to the compiler
#-Wno-deprecated-declarations tell the compiler to NOT warn about uses of functions, variables, and types marked as deprecated
#-g option allows for debugging tools
CXXFLAGS = -Wno-deprecated-declarations -g -O0

#PROJECT_PATH variable is set to CURRENT_DIR (which is taken from batch file)
PROJECT_PATH = $(CURRENT_DIR)

#COMPILE contains g++ -Wno-deprecated-declarations -g -O0 
COMPILE = $(CC) $(CXXFLAGS) $(INC_DIRS) -c 
LINK = $(CC) $(CXXFLAGS) $(LIB_DIRS)

FILES =PlayerGuessDriver.o Keyboard.o Random.o String.o ReadFile.o
EXECUTABLE = PGD.exe

all: PGD

PGD: $(FILES)
$(LINK) $(FILES) $(LIBS) -o $(EXECUTABLE)

PlayerGuessDriver.o: Keyboard.o Random.o String.o ReadFile.o
$(COMPILE) PlayerGuessDriver.cpp

Keyboard.o:
$(COMPILE) Keyboard.cpp
Random.o:
$(COMPILE) Random.cpp
String.o:
$(COMPILE) String.cpp
ReadFile.o:
$(COMPILE) ReadFile.cpp

No comments:

Post a Comment

Get the OpenURL of an Item in Primo New UI

I needed to view the OpenURL of an item in Primo New UI so that I could troubleshoot why one of our Alma General Electronic Services (ILLiad...