Objectives
We would like to :- Begin a review of C++
- Review our coding environment
- Review the edit-compile-execute cycle
- Review compiler flags
- Review a simple Makefile
Notes
- Environment
- prog1.cs.pennwest.edu
- devweb1.cis.pennwest.edu
- Both use your PennWest credentials.
- You will need to vpn in to get access from off campus.
- Prog1 has a more modern compiler, so I will be using it.
- The final arbitrator for does your program compile is prog1.
-
g++ --version
- You can access these machines a number of ways.
- I will use putty, winscp and vi.
- You will likely use visual studio code.
- But this doesn't matter as long as you can use the machine.
- Task 1: I would like to simulate a six sided die.
- How can we generate random numbers in c++?
- I assume we discuss
rand,srandand perhapstime(nullptr) - And probably some version of this code
#include <iostream> #include <iomanip> using namespace std; int main() { srand(time(nullptr)); for(int i = 0; i < 10; ++i) { cout << rand() % 6 + 1 << endl; } return 0; } - Make sure you understand rand, srand from this discussion.
- Make sure you remember/understand how to edit/compile/execute
- Make sure you understand % 6 + 1
- Compiler options
- Remember, the compiler can help us detect bad code.
- For now I want you to use
-Wall -Wextra -Wpedantic -Wshadow -Wconversion -std=c++23 -g -O2
- These will work on prog1, but not on devweb1, it doesn't have c++23
-
-Wall"This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros." -
-Wextra"This enables some extra warning flags that are not enabled by -Wall." -
-Wpedantic"Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std option used." -
-Wshadow" Warn whenever a local variable or type declaration shadows another variable, parameter, type, class member (in C++) ..., or whenever a built-in function is shadowed. Note that in C++, the compiler warns if a local variable shadows an explicit typedef, but not if it shadows a struct/class/enum." -
-Wconversion"Warn for implicit conversions that may alter a value." -
-std="Determine the language standard", this let's us use the c++23 standard -
-gSet the debugging level, -
-O2set the optimization level. - All quotes from the man page.
-
- Compile with
g++ -Wall -Wextra -Wpedantic -Wshadow -Wconversion -std=c++23 -g -O2 task1pre.cpp -o task1pre
- A simple Makefile
- Remember make is a utility for building program fro source code.
- It is very useful when you have multiple files.
- But it is also useful when you want to use a set of compiler flags too.
- Create a file called
Makefileormakefile, but notmakefile.txtorMakefile.txt - Add a line at the top of the file
-
OBJS = task1
- This assumes that
task1.cppholds your code. - You will add to this if you have other program.s
- This defines a variable that holds a list of the programs we are working with (not files)
-
- Add a line :
-
CXXFLAGS = -Wall -Wextra -Wpedantic -Wshadow -Wconversion -std=c++23 -g -O2
- This will tell make to use these flags any time it compiles a program.
-
- Add a line
-
all: ${OBJS} - This defines a target
- The target all, or the first target, is the default if you just type
makeat the command line. - ${OBJS} accesses the values in the variable OBJS, Make uses an old programming language.
-
- Add the following lines
-
clean: rm -f ${OBJS} *.o - This builds a new target, clean, which will remove all executable files (in the build list) and object files.
- Make sure that
- There is a tab before
rm, make is old and this marks the body of the target. These are the commands to apply to build that target. - There is no space in the *.o, especially after the *
- There is a tab before
-
-
OBJS = task1 CXXFLAGS = -Wall -Wextra -Wpedantic -Wshadow -Wconversion -std=c++23 -g -O2 all: ${OBJS} clean: rm -f ${OBJS} *.o