C Header Files, Libraries, and Makefiles Flashcards
(38 cards)
What should header (.h) files contain?
include statements for standard header files, common #define’s, structure definitions, typedefs, and function prototypes.
What should NOT be in header (.h) files?
Function definitions and executable code.
What is the standard naming convention for header files?
The header (.h) file is ALWAYS named after the code (.c) file it belongs to.
What is a function prototype?
A declaration of a function that specifies its name, return type, and parameter types, but not its body.
What is the purpose of the extern keyword in function prototypes?
It indicates that the function is defined elsewhere (typically in a .c file) and is globally visible.
What is a well-structured approach to organizing C program files?
Split code into multiple files: header files (.h) containing declarations and prototypes, and implementation files (.c) containing function definitions.
What problem can occur when including the same header file multiple times?
Redefinition errors for structures, typedefs, and other definitions.
How can you visualize the problem with multiple includes?
When a file includes two other files that both include the same header file, that header file’s contents get included twice.
What is the solution to the multiple includes problem?
Use #include guards in every header file.
What is the syntax for #include guards?
ifndef __HEADER_NAME_H
#define __HEADER_NAME_H
/* content of header file */
#endif
What naming convention should be used for #include guard identifiers?
Double-underscore, followed by the file name in capitals, with dots replaced by underscores (e.g., __STACK_H).
What happens if you don’t use #include guards?
Compiler will generate “redefinition” errors for structures, typedefs, and other definitions.
What is the command to compile a C file without linking?
gcc -c filename.c
What is the command to link object files into an executable?
gcc file1.o file2.o -o programName
What are the steps to compile a modular C program?
1) Compile each .c file into object files separately, 2) Link the object files together to create the executable.
What is a library in C programming?
A collection of object files grouped together into a single, searchable unit.
What are the two main types of libraries in C?
Static libraries (.a) and dynamically linked shared object libraries (.so).
What are static libraries (.a)?
Libraries that are linked with and become part of the application at link time, resulting in larger but portable executables.
What are dynamically linked shared object libraries (.so)?
Libraries that are linked to at runtime, resulting in smaller executables, but the shared object library must be available at execution.
How do you link a program to the math library?
Use the -lm flag during linking: gcc myMaths.o -lm -o myMaths
Why might this code fail to link: gcc myMaths.o -o myMaths?
If the code uses math functions like cosf(), it needs to be linked to the math library with -lm.
What is the naming convention for libraries?
Libraries are named lib<name>.a for static libraries or lib<name>.so for shared libraries, but are linked using the -l<name> flag.</name></name></name>
What is the purpose of a Makefile?
To automate the compilation and linking process, especially for projects with multiple source files and dependencies.
What are the three main components of a Makefile rule?
Targets, dependencies, and commands.