C Header Files, Libraries, and Makefiles Flashcards

(38 cards)

1
Q

What should header (.h) files contain?

A

include statements for standard header files, common #define’s, structure definitions, typedefs, and function prototypes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What should NOT be in header (.h) files?

A

Function definitions and executable code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the standard naming convention for header files?

A

The header (.h) file is ALWAYS named after the code (.c) file it belongs to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a function prototype?

A

A declaration of a function that specifies its name, return type, and parameter types, but not its body.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of the extern keyword in function prototypes?

A

It indicates that the function is defined elsewhere (typically in a .c file) and is globally visible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a well-structured approach to organizing C program files?

A

Split code into multiple files: header files (.h) containing declarations and prototypes, and implementation files (.c) containing function definitions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What problem can occur when including the same header file multiple times?

A

Redefinition errors for structures, typedefs, and other definitions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you visualize the problem with multiple includes?

A

When a file includes two other files that both include the same header file, that header file’s contents get included twice.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the solution to the multiple includes problem?

A

Use #include guards in every header file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the syntax for #include guards?

A

ifndef __HEADER_NAME_H

#define __HEADER_NAME_H
/* content of header file */
#endif

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What naming convention should be used for #include guard identifiers?

A

Double-underscore, followed by the file name in capitals, with dots replaced by underscores (e.g., __STACK_H).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What happens if you don’t use #include guards?

A

Compiler will generate “redefinition” errors for structures, typedefs, and other definitions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the command to compile a C file without linking?

A

gcc -c filename.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the command to link object files into an executable?

A

gcc file1.o file2.o -o programName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the steps to compile a modular C program?

A

1) Compile each .c file into object files separately, 2) Link the object files together to create the executable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a library in C programming?

A

A collection of object files grouped together into a single, searchable unit.

17
Q

What are the two main types of libraries in C?

A

Static libraries (.a) and dynamically linked shared object libraries (.so).

18
Q

What are static libraries (.a)?

A

Libraries that are linked with and become part of the application at link time, resulting in larger but portable executables.

19
Q

What are dynamically linked shared object libraries (.so)?

A

Libraries that are linked to at runtime, resulting in smaller executables, but the shared object library must be available at execution.

20
Q

How do you link a program to the math library?

A

Use the -lm flag during linking: gcc myMaths.o -lm -o myMaths

21
Q

Why might this code fail to link: gcc myMaths.o -o myMaths?

A

If the code uses math functions like cosf(), it needs to be linked to the math library with -lm.

22
Q

What is the naming convention for libraries?

A

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>

23
Q

What is the purpose of a Makefile?

A

To automate the compilation and linking process, especially for projects with multiple source files and dependencies.

24
Q

What are the three main components of a Makefile rule?

A

Targets, dependencies, and commands.

25
What is a target in a Makefile?
The file to be created or the action to be carried out.
26
What are dependencies in a Makefile?
Files that the target depends on, which must exist and be up-to-date before the target can be built.
27
What are commands in a Makefile?
The shell commands to execute to create the target from its dependencies. (Must be indented with tabs, not spaces.)
28
How would you declare variables in a Makefile?
Using assignments like CC = gcc and CFLAGS = -ansi.
29
How do you reference variables in a Makefile?
Using the $(variable_name) syntax, e.g., $(CC) $(CFLAGS) -c file.c
30
What does the make command do?
It reads the Makefile, checks the dependencies, and executes the commands to build the targets that are out of date.
31
What happens when you run make without specifying a target?
It builds the first target in the Makefile (the default target).
32
What is the purpose of the touch command in relation to makefiles?
It updates a file's modification time, which can be used to trigger recompilation of dependent files.
33
How does make determine if a target needs to be rebuilt?
It compares the modification times of the target and its dependencies. If any dependency is newer than the target, the target is rebuilt.
34
What is the purpose of the clean target in a Makefile?
To remove all generated files (object files, executables) so that the project can be built from scratch.
35
What are implicit rules in a Makefile?
Built-in rules that make knows about, such as how to create a .o file from a .c file.
36
What is a phony target in a Makefile?
A target that doesn't represent a file but rather an action to be performed, like clean
37
How would you write a Makefile rule to compile a .c file into a .o file?
file.o: file.c file.h $(CC) $(CFLAGS) -c file.c
38
What would happen if you run make myProgram after modifying stack.h?
All files that depend on stack.h (directly or indirectly) would be recompiled, and the executable would be relinked.