- video @youtube.de; slides @uni-freiburg.de; Exercise Sheet @uni-freiburg.de; code from the lecture @uni-freiburg.de
- comile with g++ with
g++ -c <c++ source file>
creates a .o
file
- with
nm -C <.o file>
you can see which functions are available in the file. (more info with mannm
) - link the
.o
files with
g++ <.o files>
you have to guarantee that each function is only given in one file.
- This way you can incrementally change the code and you only need to recompile the changed parts of the code
- Define the name of the executable with the
-o <name of exe>
option - Do not include other
cpp
files. Rather declare the needed functions. Example:
int functionname(<parameter types>);
- Alternatively you can move the declaration of the functions to a header file (
.h
) and the include the header file- add function descriptions only in the header file
- you can also add the
.h
files to the checkstyle - add a header guard to the header file, which prevents including cyclic inclusion of a file with compiler instructions
#ifndef <VARIABLE NAME>
#define <VARIABLE NAME>
... code ...
#endif // <VARIABLE_NAME>
- name the variable of the header guard like the header file with the relevant parts of the path
- include the header file to the source file of the function, to check if the header file is compatible with the function declaration
- add libraries with the
-l<library name>
option when linking - add libraries to the executable by linking with the
-static
option - check which libraries are needed with
ldd <name of the executable>
- structure of makefile:
<target>: <dependencies>
<commands>
note that there needs to be a tab
in front of the commands