Presentation is loading. Please wait.

Presentation is loading. Please wait.

Header files.

Similar presentations


Presentation on theme: "Header files."— Presentation transcript:

1 Header files

2 Separating code into multiple files
When dealing with larger projects, it’s advisable to divide your code into separate files based on their purpose (function sets) You can use these self-made libraries to quickly add desired functionality to your code Typically used files mycode.h – header file, typically contains function prototypes, macros, [classes,] structures mycode.c – Code that implements the functions declared in the header file 2018 Risto Heinsar

3 Ways to use multiple files
Using projects Must include headers (#include "myheader.h") Needs IDE support. E.g. Code::Blocks, Visual Studio, Eclipse etc. Compiling from the command line (small projects only) gcc -o myprogram file1.c file2.c Direct include (bad practice, never use) No need for an additional header Including a code file is sufficient (#include "mycode.c") Using Makefiles (recommended) 2018 Risto Heinsar

4 Try it out Download the sample package
Familiarize yourself with the structure of these files Compile from command line gcc -o programname file1.c file2.c -Wall Ensure that it works NB! Windows requires you to add the compiler under PATH (already set for those using geany) In Linux you don’t need to do anything extra 2018 Risto Heinsar

5 Lab task Download and open the base code from the web
Move the prototypes and other necessary items to the header file The data your going to be working with is already initialized Create the function implementations in another file, separate from main All functions except PrintStructArr() must return the found value Only function that is allowed to have print statements is PrintStructArr() The output for the rest of the functions can be done in main() If needed, you can create additional functions. Compile from command line 2018 Risto Heinsar


Download ppt "Header files."

Similar presentations


Ads by Google