140 likes | 175 Views
C++ Global functions. Declarations & Definitions Preprocessor Directives. Before there was C++. there was just plain C No classes in C Instead: structs – more on those later (maybe) No classes = no class methods Instead, there were just functions…
E N D
C++ Global functions Declarations & Definitions Preprocessor Directives CS-1030 Dr. Mark Hornick
Before there was C++ • there was just plain C • No classes in C • Instead: structs – more on those later (maybe) • No classes = no class methods • Instead, there were just functions… • Today in C++, they are called global functions • E.g. main() is a global function • …and variables CS-1030 Dr. Mark Hornick
Splitting an program among multiple source (.c) files Scenario: Divide the implementation of a program into two source files: • Demo.c defines the main() and getUserInput() and printOutput() functions • B.c defines the calculate() function In order to call calculate() from within Demo.cpp, the calculate() function must be declared in Demo.c Likewise, in order to call printOutput() from within B.c, the printOutput() function must be declared in B.c, or someplace else (like B.h) CS-1030 Dr. Mark Hornick
Declarations vs Definitions • Declarations announce the existence of something to the compiler • Declarations can be announced in many places • Definitions specify what those “somethings” are • Definitions can only be provided once CS-1030 Dr. Mark Hornick
Function declarations • Syntax • [extern] int func1(int, float); • This just announces to the compiler that, somewhere, there is a corresponding definition (containing a body) for the function CS-1030 Dr. Mark Hornick
Function definitions • Syntax • int func1(int a, float b){<C++ statements go here…>} • This defines what the function actually is CS-1030 Dr. Mark Hornick
Global variable declarations • Syntax • extern int x; • This just announces to the compiler that, somewhere, there is a corresponding definition for the variable CS-1030 Dr. Mark Hornick
Global variable definitions • Syntax int x=10; • This defines what the variable actually is • Memory for the variable is allocated • What about initialization? • Not required, but if not supplied, value is undefined CS-1030 Dr. Mark Hornick
#include directive • Typing a lot of function and variable declarations can be tedious and error-prone • Instead, we type those declarations only once in a header file, and merge the declarations in that file with those in the .c file • A header file usually has the .h file extension • The merging is done via the #include directive:#include <file.h> • A header file can recursively #include other header files • This can continue many levels deep, which is often the case CS-1030 Dr. Mark Hornick
#include details • The #include directive • Instructs the compiler to literally include the contents of the specified file • Included files are referred to as “header files” • Header files often end with .h extension • Notable exception: C++ standard library header files • Syntax 1: #include <file> • Searches directory path(s) specified by the INCLUDE environment variable • Syntax 2: #include “file” • First searches the current directory (where the .cpp file exists, then searches directory path(s) specified by the INCLUDE environment variable • Syntax 3: #include “<absolute path>” • Searches for the specific file only at the specific location • Ex: #include “C:\My Documents\sample.txt” CS-1030 Dr. Mark Hornick
C/C++ Compiler • The compiler processes each source file (.c) individually • Output is an object file (.o) • The term “object file” has nothing to do with C++ objects; the naming is a legacy from the era before OO • The compiler makes two passes through the source file • First pass inspects the file for Preprocessor Directives • #include • #define • #ifdef • #pragma • Many others… • The second pass parses the C statements and (ultimately) generates the .o file • .o contains processor-specific instructions • References to “other stuff” not found within the original source file CS-1030 Dr. Mark Hornick
#define directive • In C, used often to alias constant values to mnemonics • #define A 1 • Equates a symbol named “A” with literal 1 • Everywhere A subsequently appears, the preprocessor substitutes the literal value 1 CS-1030 Dr. Mark Hornick
#ifdef • Used for conditional compilation of code#define DEBUG 1…#ifdef DEBUG for(…) // some C code goes here#endif • The code within the #ifdef is compiled only if DEBUG is non-zero CS-1030 Dr. Mark Hornick
Preprocessor documentation • Search “preprocessor” in the online help CS-1030 Dr. Mark Hornick