1 / 9

EECS 110: Introduction to Header Files

EECS 110: Introduction to Header Files. Chris Riesbeck. Managing Big Programs. Big programs, like your game project, should be split into smaller files, for maintainability and faster compiling

tarak
Download Presentation

EECS 110: Introduction to Header Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EECS 110: Introduction to Header Files Chris Riesbeck

  2. Managing Big Programs • Big programs, like your game project, should be split into smaller files, for maintainability and faster compiling • Each file should be a coherent collection of related functions, e.g., fish drawing code, game scoring code, … • One file, e.g., main.c should define main(), which should call the functions defined in the other files.

  3. Header Files • Code files other than main.c should be split into two parts: • code.c • all actual function definitions • all function prototypes that are "private," i.e., not used by any other file • code.h • all typedef's, enum's and struct's • function prototypes for all functions in code.c used by other files, e.g., main.c

  4. Header File Structure #ifndef CODE_NAME #define CODE_NAME …structs, enums, prototypes… #endif • The #ifndef/#define/#endif instructions are a standard idiom that makes sure that this file is loaded at most once, no matter how many files #include it.

  5. Example Header File #ifndef FISH_H #define FISH_H typedef struct { int type, dir; double x_pos, y_pos, size, speed; unsigned long color; } OBJECT; void DrawFish(OBJECT *pFish); void DrawFood(OBJECT *pFood); void MoveFish(OBJECT *pFish); void MoveFood(OBJECT *pFood); #endif

  6. Using Your Header (.h) Files • Use #include "…" to include any header files you defined • Example: #include "fish.h" • Put your header files in the same folder as your C files • Use #include <…> to include standard C library header files • Example: #include <stdlib.h>

  7. Using Your Code (.c) Files • Put your code (.c) files in the same directory as the other files in your project. • Use your IDE’s “add to project” command to add the code files to your project. • Do not include code files, i.e., do not write #include "fish.c" in any file.

  8. Project Structure • Make sure all your .h and .c source files are in the same folder. • Make sure every file that uses functions from another file has an #include "….h" for that file. • Make sure that all .c files are actually in your Dev-C++ project.

  9. Example Project

More Related