Homework 1, G++ Flags and Makefile.
Short Description:
Learn about compiler flags and how to use them.
This assignment is worth 10 points.
Goals
When you finish this homework, you should have:
- Learned about some compiler flags.
- Learned how to compile using the make build system.
- Understand the importance of eliminating warnings.
Warning
The purpose of this exercise is to investigate some poor programming practices and the compiler flags that alert you to their presence. As such, this code is NOT necessarily a good example of how to write code. That is why the compiler will produce warnings about this code.
If you see examples of code that you are unfamiliar with, please read them over carefully and identify how they violate the standard practices you have been taught. All code presented is syntactically correct, however it not all the code included contains good programming practices. If you have any questions about the code, please contact your instructor.
Please note, the g++ compiler changes over time. This means that warning flags change, produce different errors, or can be completely removed. This exercise has been designed to work with gcc version 8.3.1 (currently installed on cslab103). The compiler also supports different versions of the language. Both of these items will be discussed in the exercise.
This homework is designed to be interactive, with you reading all of the parts. Please do not just answer the questions.
Setup
- You should be logged into cslab103 to do this exercise.
- You should be working in a directory or folder for this exercise.
- If you don't know how to do this, seek assistance from your instructor, a tutor, or a fellow student.
- You should have an editor and other tools required to change files on cslab103.
- You should have a word processor open to answer questions. In the end, you will need to submit a word document containing answers to questions and screen shots.
- As you go through each step, instructions will be provided at the end of the step on what you should do to receive credit for the step. Make sure that you label these in your document with the step number.
- In most terminal emulators, you can type
clear
to clear the screen for a clean screen shot.
Exercise
- What are compiler flags?
- Compiler flags allow you to change the performance of the compiler. You already probably know about one,
-o outputFileName
-
g++ -o myProg myProg.cpp
- This flag tells the compiler to name the final output the given file name.
- If it is not present, the file would be called a.out.
- Try this out.
- Download the file one.cpp.
- Compile the program with the following command
g++ one.cpp
- List the files in the directory with the command
ls
.
- Notice the file a.out was produced.
- Compile the program with the following command
g++ -o one one.cpp
- List the files in the directory with the command
ls
.
- Notice the file one was produced.
-
- You can do other things with these flags as well.
- Try
g++ --version
- Try
g++ --help
- You can obtain a full listing of the compiler flags, along with descriptions.
- Type
man g++
- This is somewhat overwhelming, but it is worth knowing about this source.
- For this step, provide a screen shot showing that you have compiled the program with and without the -o option. (This should look like the screen shot above, but have your username in the prompt)
- Language Control Flags
- There are multiple versions of the C++ language.
- Every three years, a new standard is issued, the year determines the name of the standard.
- In this class, I would like to use the standard from 2017
- This is the latest standard supported by the compiler on cslab103.
- To do this, we will compile with the flag
--std=c++17
- I am reasonably sure that the default version of c++ on cslab103 is 14.
- One of the additions to c++17 is the
gcd
function.
- The program newC.cpp uses this function.
- Download the program.
- Compile it with
g++ -o newC newC.cpp
- Compile it with
g++ -o newC --std=c++17 newC.cpp
- For this step, take a screen shot of compiling the code both ways, and comment on the error message produced.
- Warnings
- The compiler has been programmed to recognize some poor programming practices.
- When instructed to do so, it will alert you to these practices in the form of a warning.
- A warning indicates that the code is syntactically correct, but could produce unexpected results.
- Look at one.cpp. This code contains several problems.
- Notice that the variable
i
.
-
i
has been declared twice
- Once in the scope of the main function.
- A second time in the scope of the for loop.S
- This is name hiding, or a shadow variable.
- In general this is confusing and easy to cause errors.
- Secondly, the fist instance of
i
is not initialized.
- This is a huge problem and frequently leads to errors.
- The compiler can warn you about both of these problems
- Try
g++ -Wshadow one.cpp
- Read the warning, notice what it tells you about the problem.
- Try
g++ -Wuninitialized one.cpp
- Read the warning, notice what it tells you about the problem.
- For this step, include a screen shot of compiling the code as suggested above. Include the warnings.
- Warnings = errors
- It is not acceptable to ignore warning messages.
- So the compiler has a flag that turns warnings into errors.
- This is
-Werror
- Try this out
- remove one and a.out, not one.cpp
- List the files in the folder to show that there is no executable.
- Compile the code with
-Werror
-
g++ -Wshadow -Werror one.cpp
- List the files to show that a.out was not produced.
- Add a screen shot of the above steps.
- Major warning flags
- There are several flags that turn on many warnings
- While these don't provide all the potential warnings, they are very helpful finding problematic code.
- Download two.cpp
- Compile this with the command
- Notice this catches an unused parameter and the uninitialized variable, but not the shadow variable.
- It is sometimes annoying to have the unused parameter error when you are developing code.
- Simply removing the variable identifier in the function implementation will remove this error.
- Change the function implementation (not prototype) to be
- Notice this solution is only acceptable for developing code, not for finished products.
- Add a screen shot of compiling with all three flags to your document.
- Two other flags
- The
-g
flag tells the compiler to keep information helpful for symbolic debuggers.
- We will "discuss" this later in another exercise.
- The
-O
enables optimization.
- This tells to the compiler to perform basic optimization, or attempt to make the final code run faster.
- This flag is required for some warnings in some versions of the compiler.
- A Makefile
- It is somewhat difficult to type all of the flags correctly.
- A solution to this is to use a
Makefile
to control how your project is built.
- This is using the make build utility.
- To use this copy Makefile into your directory.
- This must be called Makefile
- Spelled correctly.
- With NO file extension.
- It is probably best if you just download this file, do not try to recreate it right now.
- Examine the contents of this file:
-
CXXFLAGS
is a variable in this system.
- It determines what flags the compiler users.
- This is a built in variable for make.
-
OBJS
is also a variable.
- It is user defined.
- And tells make what programs to build.
-
all
is a rule
- It allows the programmer to build all of the executables in the project.
-
clean
is a rule
- It tells make how to "clean up" the directory.
- This is a traditional rule
- It removes all executables and object files.
- Note: The second line begis with a tab character.
- For this step show a screen shot were you execute the following:
-
make clean
-
make
-
make newC
Some final notes
- I will provide compiler flags for each assignment.
- You must use these flags for a valid submission.
- You may not have errors or warnings when you compile your code with these flags.
- While I do not require you to understand what all of the flags do, it is probably a good thing for you to investigate.
- I expect you to have a Makefile to compile your code.
- I will provide makefiles for most projects.
- Use them.
Required Files
A word document containing answers to questions and screen shots.
Submission
Submit the assignment to the D2L folder homework 1 by the due date.