Headerfiles and Makefiles Flashcards
What are Header (.h) files?
Templates / short snippets of code to be implemented in a .cpp file.
Headerfiles and MakeFiles
How are header files added?
#include “header_file.h”
(“ “, not < >)
What does including a .h file accomplish in a .cpp file?
It essentially copies the .h file to the top of the .cpp file.
Headerfiles and MakeFiles
As a rule of thumb, what should header files contain?
- Include Guards
- Definitions and outline for code.
- Some small simple code, ex getters and setters.
Headerfiles and MakeFiles
Should “using namespace” be used in header files?
No, because it will add the code in the .h file to the namespace, rather than add the namespace to the global namespace like in .cpp files.
Headerfiles and MakeFiles
How do you access/implement a free floating function from a .h file?
Directly using the same footprint as defined in the .h file.
Given class method DoStuff(int i); in class MyClass declared in a .h, how would you implement this in a .cpp?
With a reference to the class name. (i.e: MyClass::DoStuff(int i) {…}) or with “using namespace MyClass” At the top of the .cpp.
What are include guards?
A pre-compiler command to tell the compiler not to duplicate .h code if already included.
What is the syntax of include guards?
#ifndef FILENAME_H
#define FILENAME_H
…some code…
#endif FILENAME_H
What is a .o file?
A file that contains the machine code representation of your code, but not yet executable.
What is a .a file?
An archive file that contains a library of compiled .o files.
What is the relation between a archive and header files?
The .h file acts as a translator between the user and the .a file.
What is a Makefile
A text file containing a list of commands
What are targets in a Makefile?
Names of Makefile Commands.
What whitespace does a Makefile use?
Tabs, not spaces.
What is the default command in a Makefile?
The first command in a Makefile, or the commands under the ‘all’ target.
What is a dependency in a Makefile?
A list of commands before the Make target can run.
What is the syntax of a Makefile dependency?
target : dependencies
ex:
HelloWorld : hello.o
If a dependency is not found as a target in a Makefile, what happens?
It searches for a file by the same name.
What is a Makefile ‘Pattern’?
A target that uses a pattern rule to streamline a command.
How is a Makefile pattern rule made syntactically?
With %. For example, %.o will look for any make command with a .o, and replace % with that command.
What does the following code do in a Makefile?
%.o : %.c
g++ –g –Wall –c $<
Looks for any make command with .o, searches for the requisite.c, and uses that .c where the $< is.
In a Makefile pattern, what does $< represent?
Any dependency file for a given target.
In a Makefile, what is the ‘.PHONY:’ command?
It tells Makefile not to expect a file to be created.
What are the 3 compilation flags required for this course?
- Wall
- Wfatal-errors
- Werror
What is the ‘Wall’ complation flag?
All of the basic warnings for unstable code.
What is the ‘Wfatal-errors’ complation flag?
Any error stops compilation.
What is the ‘Werrors’ complation flag?
Compiler will treat all warnings as errors.