Podcast
Questions and Answers
In a Makefile
, what is the primary purpose of the target?
In a Makefile
, what is the primary purpose of the target?
Which of the following best describes the role of 'prerequisites' or 'dependencies' in a Makefile
entry?
Which of the following best describes the role of 'prerequisites' or 'dependencies' in a Makefile
entry?
In a Makefile
structure, what is the purpose of the command line?
In a Makefile
structure, what is the purpose of the command line?
Examine the following Makefile
snippet:
myprog: myprog.c myprog.h
gcc myprog.c -o myprog
What must precede the gcc
command?
Examine the following Makefile
snippet:
myprog: myprog.c myprog.h
gcc myprog.c -o myprog
What must precede the gcc
command?
Signup and view all the answers
Consider a Makefile
designed to compile a C program. If the target is an executable named myprog
, and the source file myprog.c
is modified after myprog
was last compiled, what will make
do?
Consider a Makefile
designed to compile a C program. If the target is an executable named myprog
, and the source file myprog.c
is modified after myprog
was last compiled, what will make
do?
Signup and view all the answers
In the context of make
, what is the primary purpose of specifying prerequisites for a target?
In the context of make
, what is the primary purpose of specifying prerequisites for a target?
Signup and view all the answers
What action does make
take if any prerequisite file is newer than the target file?
What action does make
take if any prerequisite file is newer than the target file?
Signup and view all the answers
Why is the selective recompilation of files, as facilitated by make
, considered important for large applications?
Why is the selective recompilation of files, as facilitated by make
, considered important for large applications?
Signup and view all the answers
When defining a command line in a Makefile, what is the critical syntactical requirement for prefixing the command?
When defining a command line in a Makefile, what is the critical syntactical requirement for prefixing the command?
Signup and view all the answers
If a directory contains both a Makefile
and a makefile
, which file will make
use by default when invoked without a specific target?
If a directory contains both a Makefile
and a makefile
, which file will make
use by default when invoked without a specific target?
Signup and view all the answers
What is the purpose of suffix rules in the context of using make
?
What is the purpose of suffix rules in the context of using make
?
Signup and view all the answers
When are comments typically used in a Makefile
?
When are comments typically used in a Makefile
?
Signup and view all the answers
How can preprocessor flags be used to manage include paths in a Makefile
?
How can preprocessor flags be used to manage include paths in a Makefile
?
Signup and view all the answers
What is the function of the CC
variable within a Makefile
?
What is the function of the CC
variable within a Makefile
?
Signup and view all the answers
How do IDEs relate to makefiles and compiler flags?
How do IDEs relate to makefiles and compiler flags?
Signup and view all the answers
What is the purpose of the -Dsymbol[=value]
compiler flag?
What is the purpose of the -Dsymbol[=value]
compiler flag?
Signup and view all the answers
Which compiler flag is used to generate position-independent code, suitable for creating shared object libraries?
Which compiler flag is used to generate position-independent code, suitable for creating shared object libraries?
Signup and view all the answers
What is the function of the -Llibrary_dir
linker flag?
What is the function of the -Llibrary_dir
linker flag?
Signup and view all the answers
In a typical C project, what is the conventional location for header files that are intended for public use with the final product?
In a typical C project, what is the conventional location for header files that are intended for public use with the final product?
Signup and view all the answers
Which linker flag is used to link a math library named libmath.so
?
Which linker flag is used to link a math library named libmath.so
?
Signup and view all the answers
In a makefile
, what is the effect of the line LIB = -lm -L/usr/local/lib -L. -lmyLib
?
In a makefile
, what is the effect of the line LIB = -lm -L/usr/local/lib -L. -lmyLib
?
Signup and view all the answers
If the macro CC
is predefined as the command cc
, what happens if you set CC = gcc
in a makefile
?
If the macro CC
is predefined as the command cc
, what happens if you set CC = gcc
in a makefile
?
Signup and view all the answers
What will be the result of the macro substitution $(SRCS:.c=.o)
if SRCS = a.c b.c c.c
?
What will be the result of the macro substitution $(SRCS:.c=.o)
if SRCS = a.c b.c c.c
?
Signup and view all the answers
What is the effect of the macro substitution $(SRCS:.c= )
if SRCS = a.c b.c c.c
?
What is the effect of the macro substitution $(SRCS:.c= )
if SRCS = a.c b.c c.c
?
Signup and view all the answers
Consider the makefile
snippet:
SRCS = a.c b.c c.c
all: $(SRCS:.c = )
Why does this makefile
compile the executables a
, b
, and c
?
Consider the makefile
snippet:
SRCS = a.c b.c c.c
all: $(SRCS:.c = )
Why does this makefile
compile the executables a
, b
, and c
?
Signup and view all the answers
What is the result of using an undefined macro in a makefile
?
What is the result of using an undefined macro in a makefile
?
Signup and view all the answers
In a makefile
, what is the purpose of the -L
flag used in the LIBS
macro (e.g., -L/usr/local/lib
)?
In a makefile
, what is the purpose of the -L
flag used in the LIBS
macro (e.g., -L/usr/local/lib
)?
Signup and view all the answers
What command does the makefile
execute given the following lines?
a1: a1.c a1.h
gcc a1.c -o a1 $(LIBS)
LIBS = -lm -L/usr/local/lib -L. -lmyLib
What command does the makefile
execute given the following lines?
a1: a1.c a1.h
gcc a1.c -o a1 $(LIBS)
LIBS = -lm -L/usr/local/lib -L. -lmyLib
Signup and view all the answers
In a software project, which of the following is the MOST practical reason for organizing code and header files into separate directories?
In a software project, which of the following is the MOST practical reason for organizing code and header files into separate directories?
Signup and view all the answers
Why are macros like INC = include/
and SRC = src/
used in Makefiles?
Why are macros like INC = include/
and SRC = src/
used in Makefiles?
Signup and view all the answers
Consider a Makefile with the line StructListDemo.o: $(SRC)StructListDemo.c $(INC)LinkedListAPI.h
. What does this line MOST directly achieve?
Consider a Makefile with the line StructListDemo.o: $(SRC)StructListDemo.c $(INC)LinkedListAPI.h
. What does this line MOST directly achieve?
Signup and view all the answers
Which of the following is the MOST significant advantage of using a Makefile in a software project?
Which of the following is the MOST significant advantage of using a Makefile in a software project?
Signup and view all the answers
In the given example, what is the purpose of the -I$(INC)
flag in the compilation command?
In the given example, what is the purpose of the -I$(INC)
flag in the compilation command?
Signup and view all the answers
Flashcards
Prerequisites in Building
Prerequisites in Building
Files required for a target to be buildable, e.g., myprog.c and myprog.h must exist to build myprog.
Recompiling Logic
Recompiling Logic
Recompilation occurs if any prerequisite is newer than the target, only modified files are recompiled.
Makefile
Makefile
A special file containing targets and rules to build applications using make.
Command Line Utility
Command Line Utility
Signup and view all the flashcards
Invoking Make Command
Invoking Make Command
Signup and view all the flashcards
Suffix rules
Suffix rules
Signup and view all the flashcards
Comments in Makefiles
Comments in Makefiles
Signup and view all the flashcards
Compiler selection in Makefile
Compiler selection in Makefile
Signup and view all the flashcards
Preprocessor flags
Preprocessor flags
Signup and view all the flashcards
Flags in Makefiles
Flags in Makefiles
Signup and view all the flashcards
Include Path
Include Path
Signup and view all the flashcards
-D Flag
-D Flag
Signup and view all the flashcards
Compiler Flags
Compiler Flags
Signup and view all the flashcards
Linker Flags
Linker Flags
Signup and view all the flashcards
Code Organization
Code Organization
Signup and view all the flashcards
Curl Library
Curl Library
Signup and view all the flashcards
Makefile Structure
Makefile Structure
Signup and view all the flashcards
Makefile Macros
Makefile Macros
Signup and view all the flashcards
Target Dependencies
Target Dependencies
Signup and view all the flashcards
Compiler Command in Makefile
Compiler Command in Makefile
Signup and view all the flashcards
Target
Target
Signup and view all the flashcards
Prerequisites
Prerequisites
Signup and view all the flashcards
Command line
Command line
Signup and view all the flashcards
Tab in Makefile
Tab in Makefile
Signup and view all the flashcards
CC macro
CC macro
Signup and view all the flashcards
LD macro
LD macro
Signup and view all the flashcards
Undefined macros
Undefined macros
Signup and view all the flashcards
Macro string substitution
Macro string substitution
Signup and view all the flashcards
Library linking
Library linking
Signup and view all the flashcards
Using variables in Makefiles
Using variables in Makefiles
Signup and view all the flashcards
Study Notes
CIS*2750 Lecture 2b: Makefiles
-
Make utility executes commands from a description file
-
Primarily used to create executables, but can perform other tasks
-
Such as removing files, reporting project status, packaging files, installing files, and building libraries
-
Make utility is not unique
-
Similar to Ant utility for Java
-
CMake is considered an improved version of Make
-
Make remains a common utility due to its frequent use
Makefiles and the Compiler Toolchain
- Make utility and compiler toolchain are separate and unrelated entities
- Make is often used to automate code building, providing flags to compilers, specifying paths, and specifying library names
- Understanding the compiler toolchain is essential to effectively using makefiles and interpreting example makefiles
Make - the Vanilla Approach
- Multiple ways to achieve similar effects with makefiles
- Good for understanding varied coding approaches to makefiles
- Learn and understand the features programmers use
- Avoid treating makefiles as magic incantations
- Adopt a straightforward and simple convention for readability
- Avoid writing code not fully understood, as this usually results in poor quality code
Makefiles
- Examines dependencies between files
- Considers file dates and existence for builds
- Attempts to construct missing files
- Recompiles compiled files if a dependency is newer
Makefile Structure
- Each entry consists of three parts:
- Target
- Prerequisites/Dependencies
- Command line
- Example:
myprog: myprog.c myprog.h gcc myprog.c -o myprog
Part 1: Target
- Target is the file a makefile entry aims to create
- Typically a filename
- Can be an executable (e.g.,
myprog
) or a library (e.g.,librecord.so
)
Part 2: Prerequisites
- Prerequisites are files required for a target's creation
- Example: to build
myprog
,myprog.c
andmyprog.h
are required - If a prerequisite is newer than the target, the command line is executed
- Only necessary files are recompiled
Makefiles and Efficiency
- Recompiling only necessary files is crucial for large projects
- Avoids the slow process of recompiling everything
Part 3: Command Line
- Command line is the command executed using dependencies
- Must be prefaced with a tab character, not spaces
- Using tabs is crucial
Invoking Make
make
compiles the first target in the Makefilemake <target>
compiles the specified target
Other Common Targets
- Targets don't need to be files
- Example:
clean
target removes unnecessary files like object files and core files
Checking the Commands
- Use the
-n
flag to view commands without execution - Example:
make -n
ormake someTarget -n
Multiline Command Lines
- Separate command lines using semicolons
- Use backslashes to continue commands onto subsequent lines
- Backslashes should always be the last character on each line
Makefile Macros
- Used to avoid repetitive text in Makefiles
- Simplifies paths, compiler flags, and library lists
- Defined using the equal sign (e.g.,
LIBS = -L/usr/local/lib -lm -llibname
) - Referenced using $ and brackets (e.g.,
$(LIBS)
or${LIBS}
)
Example: Sample Makefile
- Macro names are typically in uppercase
- Example incorporating macros in a Makefile
Undefined Macros
- Undefined macros are replaced with an empty string.
Predefined Macros
CC
is predefined and commonly corresponds to the C compiler
Macro String Substitutions
- Allows substituting strings within macros
- Example using substitution:
$(SRCS: .c = .o)
translates.c
source files to object files
Suffix Rules
- Suffix rules instruct the system on how to compile different file types
- (e.g.,
.c
to executable)
Comments
- Comments begin with a
#
Flags Control Options for Tools
- Makefiles allow controlling C toolchain components
- Some Integrated Development Environments (IDEs) handle flag settings differently, often through graphical user interfaces.
Selecting the Compiler in a Makefile
- Choosing default C compilers or using alternative ones
- Example:
CC = gcc
Preprocessor Flags
- Modifies how the preprocessor interprets the input
- Example:
-Iinclude_file_dir
to modify include paths
Compiler Flags
- Modifies compiler behavior (Optimization, debugging,...).
- Example:
-g
,-On
,-Wall -std=c11
,-fpic
,-C
Linker Flags
- Modifies linker behavior
- Example:
-Llibrary_dir
,-llibrary
,-shared
,-o filename
A Note on Code Organization
- Large C codebases often use different directories following conventions
- Headers typically placed in an include directory
- Source files in a src directory
Real-World Example: The Curl Library
- Example demonstrates a real-world Makefile usage in a large software project.
Standards for Our Course
- Course follows standard makefile conventions
- Code, headers, and the Makefile itself follow file structure conventions, and the file structure for examples are standardized
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge on the essential concepts of Makefiles, including targets, prerequisites, and command line syntax. This quiz covers various aspects of building and managing projects using Makefiles, particularly in C programming. Perfect for beginners looking to solidify their understanding of Makefile structures.