Skip to content

Files

Latest commit

7037f11 · Mar 26, 2017

History

History
49 lines (43 loc) · 2.16 KB

l2.md

File metadata and controls

49 lines (43 loc) · 2.16 KB

2. Vorlesung

g++ -c <c++ source file>

creates a .o file

  • withnm -C <.o file> you can see which functions are available in the file. (more info with man nm)
  • 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>);

Header files

  • 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

Libraries

  • 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>

Makefiles

  • structure of makefile:
<target>: <dependencies>
   <commands>

note that there needs to be a tab in front of the commands