Podcast
Questions and Answers
In the context of make
, what is the primary purpose of prerequisites?
In the context of make
, what is the primary purpose of prerequisites?
- To indicate which files should be included in the final distribution package.
- To list the files that must exist in order for the target to be considered 'buildable'. (correct)
- To specify the compiler flags used when building the target.
- To define the environment variables required for the build process.
How does make
determine whether a target needs to be recompiled?
How does make
determine whether a target needs to be recompiled?
- By checking if the source code has been pushed to a remote repository.
- By analyzing the compiler's output for any warnings or errors.
- By comparing the modification times of the target and its prerequisites. (correct)
- By always recompiling the target every time `make` is invoked.
Why is recompiling only the necessary files important for large applications?
Why is recompiling only the necessary files important for large applications?
- It simplifies the debugging process.
- It allows for better code optimization.
- It reduces the size of the executable file.
- It speeds up the compilation process. (correct)
What is a crucial requirement for the command line in a Makefile?
What is a crucial requirement for the command line in a Makefile?
If a directory contains both a Makefile
and a makefile
, which one will make
use by default?
If a directory contains both a Makefile
and a makefile
, which one will make
use by default?
Consider a Makefile
with the following targets:
myprog: myprog.c
gcc -o myprog myprog.c
fred: fred.c
gcc -o fred fred.c
What command would you use to build fred
?
Consider a Makefile
with the following targets:
myprog: myprog.c
gcc -o myprog myprog.c
fred: fred.c
gcc -o fred fred.c
What command would you use to build fred
?
Which of the following scenarios would trigger a recompilation of a target by make
?
Which of the following scenarios would trigger a recompilation of a target by make
?
When writing a Makefile
, what happens if you accidentally use spaces instead of a tab character before a command?
When writing a Makefile
, what happens if you accidentally use spaces instead of a tab character before a command?
In a Makefile
, what is the primary role of the 'target'?
In a Makefile
, what is the primary role of the 'target'?
Which compiler flag is used to include a directory in the search path for header files?
Which compiler flag is used to include a directory in the search path for header files?
Which of the following best describes the purpose of 'prerequisites or dependencies' in a Makefile
entry?
Which of the following best describes the purpose of 'prerequisites or dependencies' in a Makefile
entry?
What is the expected syntax to separate the target from its prerequisites in a Makefile
entry?
What is the expected syntax to separate the target from its prerequisites in a Makefile
entry?
What is the purpose of the compiler flag -Dsymbol[=value]
?
What is the purpose of the compiler flag -Dsymbol[=value]
?
In a Makefile
, what significance does a tab character have at the beginning of a line?
In a Makefile
, what significance does a tab character have at the beginning of a line?
Which compiler flag, when set, is designed to disable assertion checks in C code?
Which compiler flag, when set, is designed to disable assertion checks in C code?
Considering the Makefile
entry myprog: myprog.c myprog.h
, what does this signify?
Considering the Makefile
entry myprog: myprog.c myprog.h
, what does this signify?
Analyze the flags: -Wall -std=c11
. What aspect of the code building process do they control?
Analyze the flags: -Wall -std=c11
. What aspect of the code building process do they control?
What command line argument should be used to create a shared object library?
What command line argument should be used to create a shared object library?
Given the Makefile
snippet:
myprog: myprog.c myprog.h
gcc myprog.c -o myprog
What would happen if you run make myprog
and myprog
already exists and is newer than both myprog.c
and myprog.h
?
Given the Makefile
snippet:
myprog: myprog.c myprog.h
gcc myprog.c -o myprog
What would happen if you run make myprog
and myprog
already exists and is newer than both myprog.c
and myprog.h
?
In a Makefile
, what would be the effect of omitting the tab character before the command gcc myprog.c -o myprog
?
In a Makefile
, what would be the effect of omitting the tab character before the command gcc myprog.c -o myprog
?
If you want to link the libmath.so
library during compilation, which linker flag would you use?
If you want to link the libmath.so
library during compilation, which linker flag would you use?
Which of the following scenarios explains the use of the -L
linker flag?
Which of the following scenarios explains the use of the -L
linker flag?
Which of the following is NOT a typical use case for a target in a Makefile
?
Which of the following is NOT a typical use case for a target in a Makefile
?
In a typical C project structure, what is the conventional directory for storing header files that are intended for public use with the final product?
In a typical C project structure, what is the conventional directory for storing header files that are intended for public use with the final product?
How can macros improve the build process in a project?
How can macros improve the build process in a project?
In a software project, what is the primary purpose of separating code into src
and include
directories?
In a software project, what is the primary purpose of separating code into src
and include
directories?
What is the role of a Makefile
in a software project?
What is the role of a Makefile
in a software project?
In the context of a Makefile
, what is the significance of the -I
flag used with the compiler (CC
)?
In the context of a Makefile
, what is the significance of the -I
flag used with the compiler (CC
)?
Why is it beneficial to have a consistent file structure and Makefile
across different projects in a course?
Why is it beneficial to have a consistent file structure and Makefile
across different projects in a course?
In a makefile
, if the macro CC
is redefined as CC = gcc
, what effect does this have on the compilation process?
In a makefile
, if the macro CC
is redefined as CC = gcc
, what effect does this have on the compilation process?
What is the result of using an undefined macro in a makefile
?
What is the result of using an undefined macro in a makefile
?
What is the purpose of the -L
flag in the LIBS
macro within a makefile
?
What is the purpose of the -L
flag in the LIBS
macro within a makefile
?
Consider the macro substitution $(SRCS:.c=.o)
in a makefile
. If SRCS = a.c b.c c.c
, what will be the result of this substitution?
Consider the macro substitution $(SRCS:.c=.o)
in a makefile
. If SRCS = a.c b.c c.c
, what will be the result of this substitution?
Given the makefile
snippet:
SRCS = a.c b.c c.c
all: $(SRCS:.c = )
Why does this makefile
compile the executables a
, b
, and c
?
Given the makefile
snippet:
SRCS = a.c b.c c.c
all: $(SRCS:.c = )
Why does this makefile
compile the executables a
, b
, and c
?
In a makefile
, what is the effect of including -lmyLib
in the LIBS
macro?
In a makefile
, what is the effect of including -lmyLib
in the LIBS
macro?
Which of the following is the primary purpose of macro string substitutions in a makefile
?
Which of the following is the primary purpose of macro string substitutions in a makefile
?
What is the primary purpose of using macros in a makefile
?
What is the primary purpose of using macros in a makefile
?
When the command $(CC) prog.c -o prog $(LIBS)
is executed in a makefile
, and LIBS
is defined as -lm -L/usr/local/lib -L. -lmyLib
, what is the linker's library search order?
When the command $(CC) prog.c -o prog $(LIBS)
is executed in a makefile
, and LIBS
is defined as -lm -L/usr/local/lib -L. -lmyLib
, what is the linker's library search order?
Which command displays the commands that make
would execute without actually running them for a specific target named myTarget
?
Which command displays the commands that make
would execute without actually running them for a specific target named myTarget
?
In a makefile
, how can multiple commands be written on separate lines for a single target?
In a makefile
, how can multiple commands be written on separate lines for a single target?
If a makefile
contains a target without any dependencies, what happens when make
is executed without specifying any target?
If a makefile
contains a target without any dependencies, what happens when make
is executed without specifying any target?
What is the typical purpose of a clean
target in a makefile
?
What is the typical purpose of a clean
target in a makefile
?
Given the macro definition LIBS = -L/usr/local/lib -lm -llibname
in a makefile
, how would this macro be correctly referenced in a command?
Given the macro definition LIBS = -L/usr/local/lib -lm -llibname
in a makefile
, how would this macro be correctly referenced in a command?
In a makefile
, what is the effect of the following macro definition: CC = gcc
?
In a makefile
, what is the effect of the following macro definition: CC = gcc
?
Examine the following makefile
snippet:
libawesome.a: awesome.c
gcc awesome.c -o awesome.o -c ; \
ar cr libawesome.a awesome.o
What is the purpose of the backslash (\
) at the end of the gcc
command line?
Examine the following makefile
snippet:
libawesome.a: awesome.c
gcc awesome.c -o awesome.o -c ; \
ar cr libawesome.a awesome.o
What is the purpose of the backslash (\
) at the end of the gcc
command line?
Flashcards
Makefile Target
Makefile Target
The element a Makefile entry aims to build, often a filename.
Makefile Prerequisites
Makefile Prerequisites
Files or other targets that the target depends on. If these are newer than the target, the command line is executed.
Makefile Command Line
Makefile Command Line
The shell command(s) executed to build the target. Must be preceded by a tab character.
Makefile Tab
Makefile Tab
Signup and view all the flashcards
Makefile Target Details
Makefile Target Details
Signup and view all the flashcards
Makefile Dependencies
Makefile Dependencies
Signup and view all the flashcards
Makefile Command Line Details
Makefile Command Line Details
Signup and view all the flashcards
Makefile command indent
Makefile command indent
Signup and view all the flashcards
What is curl?
What is curl?
Signup and view all the flashcards
Course code standards
Course code standards
Signup and view all the flashcards
Makefile Macros
Makefile Macros
Signup and view all the flashcards
INC Macro
INC Macro
Signup and view all the flashcards
SRC Macro
SRC Macro
Signup and view all the flashcards
Default make
target
Default make
target
Signup and view all the flashcards
Non-file targets
Non-file targets
Signup and view all the flashcards
clean
target
clean
target
Signup and view all the flashcards
make -n
make -n
Signup and view all the flashcards
Semicolon (;)
Semicolon (;)
Signup and view all the flashcards
Backslash ()
Backslash ()
Signup and view all the flashcards
Macro syntax
Macro syntax
Signup and view all the flashcards
Prerequisites in Makefiles
Prerequisites in Makefiles
Signup and view all the flashcards
Makefile Dependency Check
Makefile Dependency Check
Signup and view all the flashcards
Efficiency of Makefiles
Efficiency of Makefiles
Signup and view all the flashcards
Command Line in Makefiles
Command Line in Makefiles
Signup and view all the flashcards
Command Line Prefix requirement
Command Line Prefix requirement
Signup and view all the flashcards
Typing 'make' Alone
Typing 'make' Alone
Signup and view all the flashcards
Typing 'make target'
Typing 'make target'
Signup and view all the flashcards
Example of make
Example of make
Signup and view all the flashcards
Makefile
Makefile
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
$(SRCS:.c =.o)
$(SRCS:.c =.o)
Signup and view all the flashcards
$(SRCS:.c = )
$(SRCS:.c = )
Signup and view all the flashcards
Suffix Rules
Suffix Rules
Signup and view all the flashcards
-I/path/to/include
-I/path/to/include
Signup and view all the flashcards
-Dsymbol[=value]
-Dsymbol[=value]
Signup and view all the flashcards
-DNDEBUG
-DNDEBUG
Signup and view all the flashcards
-g
-g
Signup and view all the flashcards
-On
-On
Signup and view all the flashcards
-Wall -std=c11
-Wall -std=c11
Signup and view all the flashcards
-Llibrary_dir
-Llibrary_dir
Signup and view all the flashcards
-llibrary
-llibrary
Signup and view all the flashcards
Study Notes
- The
make
utility executes a sequence of commands found in a description file. - It can be used to create executables and perform other tasks such as removing Files, reporting project status, package files, install, build libraries and more.
Alternatives to Make
- The
ant
utility is similar for Java Cmake
is supposed to be an improved makemake
is still the most common
Makefiles and Compiler Toolchains
- The
make
is separate from the compiler toolchain - It automates code building by passing flags to compilers, specifying paths, and setting library names.
- It's essential to comprehend how the compiler toolchain functions to employ makefiles effectively.
Using Make
- There are many approaches to makefile creation.
- Programmers usually have their own preferred coding approaches.
- Exposure helps learning and avoids treating makefiles as "magical incantations."
- It is best use to very straightforward conventions for readability.
Examining Files
- Makefiles examine dependencies between files, including their dates and existence.
- If files do not exist, make attempts to build them
- If dependencies have newer dates than compiled files, recompilation occurs.
Makefile Structure
- Consists of three parts
- Target
- Prerequisites or Dependencies
- Command Line
Makefile Target
- It is what a makefile entry builds
- Targets are typically filenames, such as executables or libraries
- Example being
myprog
orlibrecord.so
Prerequisites
- These are files that must exist for a target to be buildable; for example, building myprog may require
myprog.c
andmyprog.h
- The command line executes if any prerequisites are newer than the target
- Only the files needing recompilation are recompiled.
Makefiles and Efficiency
- Recompiling files that require recompilation is beneficial for large applications.
Command Line
- It's typically a Unix/Linux command-line utility with arguments to build the target using dependencies.
- The command line needs a single tab character before it, not spaces.
Invoking Make
- Typing make alone will search for a
Makefile
ormakefile
and build the first target. - Typing
make <target>
will build the specified target directly.
Typing Make Examples
make myprog
buildsmyprog
make fred
builds fred- Just typing
make
buildsmyprog
since it's the first target in the makefile.
Common Targets
- Targets don't always have to be files
- For example,
clean
is to clean up files - In examples,
clean
typically deletes: all targets, executables, library files, temporary.o
files, and the core file.
Checking Commands
- The
-n
flag displays commands make would execute without executing them make -n
displays commands for the first targetmake someTarget -n
displays commands forsomeTarget
Multiline Commands
- Multiple lines can be in the command line, using a semi-colon to separate them.
libawesome.a: awesome.c
followed by commands on separate lines- Backslashes mean to continue to the next line but it must be the last character on the line
Makefile Macros
- Macros are to avoid repeatedly typing text in makefiles.
- Paths, compiler flags, and lists of libraries often appear in a makefile, and may be annoying to retype.
- Macros replace long strings
- Defined with
e.g. LIBS = -L/usr/local/lib -lm -llibname
- Referenced
$(LIBS)
or${LIBS}
Sample Macro Example
- Macro names are in uppercase normally
CC = gcc
LIBS = -lm -L/usr/local/lib -L. -lmyLib
prog: prog.c
$ (CC) prog.c -o prog $ (LIBS)
Sample Macro Example Specifics
- Compiles using gcc, but CC macro can change to specify an alternative compiler like clang
- Links libraries in m, i.e C math library, and
myLib
- The linker will look for the search path for libraries in /usr/local/lib and the current directory, in addition to its standard search path
Undefined Macros
- Undefined macros replaced with a null string
gcc a1.o -o a1
will execute since the command uses an undefined macro
Predefined Macros
- Macro
CC
is predefined as commandcc
; it's usually a symbolic link to the distribution's default C compiler (*nix). - It's commonly either GNU C or the LLVM C compiler.
- Macro
LD
is predefined as commandld
. - These can be used without definition, or replace them with something else.
Macro String Substitution
- Allows using a macro and substituting a string.
- In the
SRCS = a.c b.c c.c
example, SRCS can be referenced with $ (SRCS: .c = .०)
translates the list to:a.o b.o c.o
- Executable names are created using source filenames
a.c
,b.c
, andc.c
. - These allow you to specify prerequisites
- Reduce clutter in a makefile for large projects with many prerequisites.
More on Macro String Substitution
$ (SRCS: .c = )
translates to:a b c
- Using names of source files can create names of executables
Suffix Rules
- They tell the system how to compile different types of files automatically
- C files typically end with
.c
, Fortran files with.f
, and C++ files with.cc
. - Make has built-in rules to turn a source code file into an executable.
- Type
make -p
to see the predefined macros
Comments
- Comments start with
#
and continue until the end of the line.
Flags Control Options
- The C toolchain components are controlled using makefiles
- Some IDEs let you change the flag by selecting options in property pages
- Others--multi-platform ones--use make and provide lists of flags to modify.
Selecting Compiler
- Using
CC=gcc
will select the gcc compiler. - On a Mac, the command line "gcc" is an alias for the Clang compiler, unless you specifically install GNU gcc.
- Pass options to elements of the compiler toolchain.
Preprocessor Flags
- Preprocessor:
CPPFLAGS=
-Iinclude_file_dir
will add the file of the directory to the include paths of both includes<>
and""
, this helps avoid hardcoding relative with the#include
statements with-I~/myproj/include
it will add to the path of the source files
Useful Preprocessor Flags
-Dsymbol[=value]
is equivalent to#define symbol value
-DNDEBUG
disables assertions within the code
Compiler Flags
- Compiler:
CFLAGS=
-g
saves symbol for debugger-On
optimization level (0,1,2,3)-Wall -std=c11
all warnings, C11 standard.-fpic
position-independent code for shared object libraries-c
compiles.o
file.- The
-c
flag goes in the command, compiling to.o
file.
Linker Flags
- Linker:
LDFLAGS=
-Llibrary_dir
will pass a library path to the linker-L~/myproj/lib
adds as containing linked files in library.
-llibrary
, links library-Ifoo
linkslibfoo.so
(or.a
).
-shared
creates shared object library These flags are used in the actual command-o filename
creates output file with name, instead of default names-o caltest
creates an output file with the namecaltest
Code Organization
- Large C codebases are often broken up into folders
- Headers go into the
include
directory - Source code goes into the
src
directory - Binary Files go into folders like
bin
,lib
etc
Real World Project ex Curl
- The curl library uses the code structure
- The libraries used will follow the specific type of code structure which depends on the project.
Standard Code organization for the Course
- They are to follow a similar convention in their code
- The code goes in src
- Header files go in include
- Main makefile is in
main
- Assignment description will explain exact details
Using Makefile Macros Code Examples
- Use macros to define locations of code and headers.
INC = include/
SRC = src/
- Use macros in makefile
- The dependencies can be the command line
-
$ (CC) $ (CFLAGS) –I$ (INC) -c $ (SRC) StructListDemo.c
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore Makefiles: how make
uses prerequisites to determine if a target needs recompilation. Understand the effects of spaces vs tabs and the function of compiler flags. Learn how to build specific targets and the importance of selective recompilation in large projects.