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.