Makefiles: Targets, Prerequisites, and Compilation
45 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

  • 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?

  • 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?

<p>It must be preceded by a single tab character. (C)</p> Signup and view all the answers

If a directory contains both a Makefile and a makefile, which one will make use by default?

<p><code>Makefile</code> (A)</p> Signup and view all the answers

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?

<p><code>make fred</code> (D)</p> Signup and view all the answers

Which of the following scenarios would trigger a recompilation of a target by make?

<p>The target file is older than at least one of its prerequisites. (A)</p> Signup and view all the answers

When writing a Makefile, what happens if you accidentally use spaces instead of a tab character before a command?

<p>The <code>make</code> utility will likely output an error and halt execution. (D)</p> Signup and view all the answers

In a Makefile, what is the primary role of the 'target'?

<p>Indicating the element that the <code>Makefile</code> entry intends to create or update. (B)</p> Signup and view all the answers

Which compiler flag is used to include a directory in the search path for header files?

<p>-I/path/to/include (A)</p> Signup and view all the answers

Which of the following best describes the purpose of 'prerequisites or dependencies' in a Makefile entry?

<p>They specify the files or targets that must exist or be up-to-date before the target can be built. (B)</p> Signup and view all the answers

What is the expected syntax to separate the target from its prerequisites in a Makefile entry?

<p>A colon (:) (D)</p> Signup and view all the answers

What is the purpose of the compiler flag -Dsymbol[=value]?

<p>It defines a macro symbol with an optional value. (B)</p> Signup and view all the answers

In a Makefile, what significance does a tab character have at the beginning of a line?

<p>It precedes a command line instruction. (B)</p> Signup and view all the answers

Which compiler flag, when set, is designed to disable assertion checks in C code?

<p>-DNDEBUG (A)</p> Signup and view all the answers

Considering the Makefile entry myprog: myprog.c myprog.h, what does this signify?

<p><code>myprog</code> depends on <code>myprog.c</code> and <code>myprog.h</code>. (B)</p> Signup and view all the answers

Analyze the flags: -Wall -std=c11. What aspect of the code building process do they control?

<p>Code standardization and diagnostics (B)</p> Signup and view all the answers

What command line argument should be used to create a shared object library?

<p>-shared (B)</p> Signup and view all the answers

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?

<p>The <code>make</code> utility will output a message stating that <code>myprog</code> is up to date and do nothing. (C)</p> Signup and view all the answers

In a Makefile, what would be the effect of omitting the tab character before the command gcc myprog.c -o myprog?

<p>The <code>make</code> utility would report an error because the syntax is incorrect. (A)</p> Signup and view all the answers

If you want to link the libmath.so library during compilation, which linker flag would you use?

<p>-lmath (B)</p> Signup and view all the answers

Which of the following scenarios explains the use of the -L linker flag?

<p>Adding a directory to the library search path. (D)</p> Signup and view all the answers

Which of the following is NOT a typical use case for a target in a Makefile?

<p>Defining a variable used throughout the <code>Makefile</code>. (B)</p> Signup and view all the answers

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?

<p>include/ (B)</p> Signup and view all the answers

How can macros improve the build process in a project?

<p>By defining locations of code and header files, enabling easier modification and consistency in build commands. (C)</p> Signup and view all the answers

In a software project, what is the primary purpose of separating code into src and include directories?

<p>To organize code and header files, improving maintainability and readability. (C)</p> Signup and view all the answers

What is the role of a Makefile in a software project?

<p>To automate the build process, specifying compilation and linking steps. (C)</p> Signup and view all the answers

In the context of a Makefile, what is the significance of the -I flag used with the compiler (CC)?

<p>It indicates the directory where header files should be searched. (C)</p> Signup and view all the answers

Why is it beneficial to have a consistent file structure and Makefile across different projects in a course?

<p>It promotes uniformity and allows students to apply learned concepts more easily across different assignments. (D)</p> Signup and view all the answers

In a makefile, if the macro CC is redefined as CC = gcc, what effect does this have on the compilation process?

<p>It explicitly specifies that the GNU C compiler should be used, overriding the default compiler. (D)</p> Signup and view all the answers

What is the result of using an undefined macro in a makefile?

<p>The undefined macro will be replaced with a null string. (D)</p> Signup and view all the answers

What is the purpose of the -L flag in the LIBS macro within a makefile?

<p>It defines the location where the linker should search for libraries. (C)</p> Signup and view all the answers

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?

<p>a.o b.o c.o (C)</p> Signup and view all the answers

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?

<p>Because <code>make</code> has implicit rules to compile <code>.c</code> files to executables when listed as prerequisites. (D)</p> Signup and view all the answers

In a makefile, what is the effect of including -lmyLib in the LIBS macro?

<p>It instructs the linker to link the program with the <code>myLib</code> library. (B)</p> Signup and view all the answers

Which of the following is the primary purpose of macro string substitutions in a makefile?

<p>To manipulate strings within macro definitions, such as changing file extensions. (A)</p> Signup and view all the answers

What is the primary purpose of using macros in a makefile?

<p>To avoid repeatedly typing long strings of text, such as paths or compiler flags. (D)</p> Signup and view all the answers

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?

<p><code>/usr/local/lib</code>, current directory, standard system library paths, math library, then <code>myLib</code>. (A)</p> Signup and view all the answers

Which command displays the commands that make would execute without actually running them for a specific target named myTarget?

<p><code>make myTarget -n</code> (A)</p> Signup and view all the answers

In a makefile, how can multiple commands be written on separate lines for a single target?

<p>By separating each command with a semi-colon <code>;</code> and using a backslash <code>\</code> at the end of each line to indicate continuation. (D)</p> Signup and view all the answers

If a makefile contains a target without any dependencies, what happens when make is executed without specifying any target?

<p>The first target in the <code>makefile</code> is built. (C)</p> Signup and view all the answers

What is the typical purpose of a clean target in a makefile?

<p>To remove files that are no longer needed, such as object files, executables, and core files. (C)</p> Signup and view all the answers

Given the macro definition LIBS = -L/usr/local/lib -lm -llibname in a makefile, how would this macro be correctly referenced in a command?

<p>${LIBS} (B)</p> Signup and view all the answers

In a makefile, what is the effect of the following macro definition: CC = gcc?

<p>It sets the compiler to be used for compiling source code to <code>gcc</code>, which can be referenced later using <code>$(CC)</code>. (A)</p> Signup and view all the answers

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?

<p>It signals that the command continues on the next line; allowing a single logical command to span multiple lines for readability. (A)</p> Signup and view all the answers

Flashcards

Makefile Target

The element a Makefile entry aims to build, often a filename.

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

The shell command(s) executed to build the target. Must be preceded by a tab character.

Makefile Tab

A character that must precede the command line in a Makefile. Indicates to Make that this line is a command to be executed.

Signup and view all the flashcards

Makefile Target Details

The first part of a Makefile entry, specifying what the entry aims to build. It is typically a file name, such as an executable or library.

Signup and view all the flashcards

Makefile Dependencies

The element listing what a target depends on. These are files that make checks to see if they are newer than the target.

Signup and view all the flashcards

Makefile Command Line Details

The part of the Makefile entry that contains the commands to be executed to build the target.

Signup and view all the flashcards

Makefile command indent

Character to start every command line in the Makefile. Without indenting the command line, make cannot understand the file.

Signup and view all the flashcards

What is curl?

A library used for command-line URL data transfer.

Signup and view all the flashcards

Course code standards

Source code will be placed in the src directory, headers in the include directory, and a Makefile in the main directory.

Signup and view all the flashcards

Makefile Macros

Macros in Makefiles are used to define locations of code and headers.

Signup and view all the flashcards

INC Macro

INC = include/

Signup and view all the flashcards

SRC Macro

SRC = src/

Signup and view all the flashcards

Default make target

The make command executes the first target in the makefile by default.

Signup and view all the flashcards

Non-file targets

Targets that aren't files, used for actions like removing temporary files.

Signup and view all the flashcards

clean target

Removes object files, executables and core dump files.

Signup and view all the flashcards

make -n

View commands that make would execute without actually running them.

Signup and view all the flashcards

Semicolon (;)

Separate commands on one line in a makefile

Signup and view all the flashcards

Backslash ()

Continue a command line onto the next line in a makefile.

Signup and view all the flashcards

Macro syntax

Defined using = and referenced using $(MACRO_NAME) or ${MACRO_NAME}.

Signup and view all the flashcards

Prerequisites in Makefiles

Files that must exist for a target to be buildable.

Signup and view all the flashcards

Makefile Dependency Check

If any prerequisite is newer than the target, the command line is executed to rebuild the target.

Signup and view all the flashcards

Efficiency of Makefiles

Recompiling only necessary files saves time, especially in large projects.

Signup and view all the flashcards

Command Line in Makefiles

The command that builds the target from its dependencies, typically a compiler command.

Signup and view all the flashcards

Command Line Prefix requirement

Must be prefaced by a single tab character (not spaces).

Signup and view all the flashcards

Typing 'make' Alone

Looks for 'Makefile' or 'makefile' and builds the first target in the file.

Signup and view all the flashcards

Typing 'make target'

Builds the target specifically named in the command.

Signup and view all the flashcards

Example of make

Typing make myprog will build 'myprog', based on the rules defined.

Signup and view all the flashcards

Makefile

Specifies compiler, source files, and libraries for building a program. Defines dependencies and build commands.

Signup and view all the flashcards

LD Macro

LD is a macro representing the linker command, used to combine compiled object files into an executable.

Signup and view all the flashcards

Undefined Macros

Macros replaced with nothing. Prevents errors if a variable isn't needed in every environment.

Signup and view all the flashcards

Macro String Substitution

Use a macro and change part of it. Modify file extensions for compiling.

Signup and view all the flashcards

$(SRCS:.c =.o)

Transforms 'a.c b.c c.c' to 'a.o b.o c.o' for object file compilation.

Signup and view all the flashcards

$(SRCS:.c = )

Turns 'a.c b.c c.c' into 'a b c', extracting names.

Signup and view all the flashcards

Suffix Rules

Infers compilation commands based on file extensions. Simplifies build process.

Signup and view all the flashcards

-I/path/to/include

Adds the specified directory to the compiler's include path, allowing header files in that directory to be included using #include "filename.h".

Signup and view all the flashcards

-Dsymbol[=value]

Defines a preprocessor symbol with an optional value, equivalent to #define symbol value. Useful for conditional compilation.

Signup and view all the flashcards

-DNDEBUG

Disables assertion checks in the code. Assertions are typically used for debugging and are removed in release builds.

Signup and view all the flashcards

-g

Saves debugging symbols in the compiled executable, essential for debugging with tools like gdb.

Signup and view all the flashcards

-On

Sets the optimization level for the compiler (0, 1, 2, or 3) to improve performance. Higher levels may take longer to compile.

Signup and view all the flashcards

-Wall -std=c11

Enables all compiler warnings and enforces the C11 standard for the code.

Signup and view all the flashcards

-Llibrary_dir

Adds the specified directory to the linker's library search path, enabling the linker to find libraries in that directory.

Signup and view all the flashcards

-llibrary

Links the specified library into the executable. The linker searches for liblibrary.so or liblibrary.a.

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 make
  • make 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 or librecord.so

Prerequisites

  • These are files that must exist for a target to be buildable; for example, building myprog may require myprog.c and myprog.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 or makefile and build the first target.
  • Typing make <target> will build the specified target directly.

Typing Make Examples

  • make myprog builds myprog
  • make fred builds fred
  • Just typing make builds myprog 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 target
  • make someTarget -n displays commands for someTarget

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 command cc; 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 command ld.
  • 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, and c.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 links libfoo.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 name caltest

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.

Quiz Team

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.

More Like This

Makefile Basics Quiz
33 questions

Makefile Basics Quiz

ComfortableBowenite2676 avatar
ComfortableBowenite2676
CURL Library & Makefile Directives
41 questions
Use Quizgecko on...
Browser
Browser