Understanding Makefiles and Compilation
38 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

What is the primary purpose of a Makefile?

  • To manage the compilation and linking process of a project, based on file modification times. (correct)
  • To create a backup of all source code files.
  • To automatically generate C source code.
  • To execute shell commands in a specific order.

What command is used to trigger the execution of rules defined in a Makefile?

  • `make` (correct)
  • `run`
  • `compile`
  • `execute`

If vec3.h is modified after a successful compilation, what will happen when make is called again?

  • The `so_game` executable will be relinked, but no recompilation will occur.
  • Nothing, because Make only checks modification times of `.c` files.
  • Only `vec3.h` will be recompiled.
  • `vec3.o` and any files that depend on `vec3.h` will be recompiled. (correct)

In the provided Makefile snippet, what does the $(LIBS) variable likely represent in the context of the final linking command for so_game?

<p>A list of external libraries to be linked with the executable. (D)</p> Signup and view all the answers

Examine the following Makefile snippet:

so_game: vec3.o surface.o image.o vehicle.o \
 world.o helpers.o so_game.o
 gcc $(CCOPTS) -o so_game vec3.o surface.o \
 image.o vehicle.o world.o helpers.o \
 so_game.o $(LIBS)

Assuming all object files are up-to-date, yet the recompilation of one source file is forced using touch <source_file>.c, describe the sequence through which make rebuilds the so_game executable.

<p>The touched source file's corresponding object file is rebuilt, and <code>so_game</code> is relinked using all object files. (B)</p> Signup and view all the answers

Which file extension typically denotes a header file in C++?

<p>.hpp (C)</p> Signup and view all the answers

What is the primary requirement for a piece of software to 'happen' or execute?

<p>It must be executed by the CPU. (C)</p> Signup and view all the answers

What is the primary function of the g++ -c command?

<p>Compiling a source file into an object file. (C)</p> Signup and view all the answers

In a Makefile, what is the purpose of the $(name) syntax?

<p>It expands a variable declaration. (A)</p> Signup and view all the answers

What is the role of an interpreter when running a script?

<p>To directly execute the source file, parsing and executing code on the spot. (C)</p> Signup and view all the answers

Which of the following is the most accurate description of 'Just In Time Compilation'?

<p>A technique where code chunks are translated into native machine code during runtime for faster execution. (D)</p> Signup and view all the answers

Given the Makefile snippet: vehicle.o: vehicle.c vehicle.h surface.h vec3.h image.h gcc $(CCOPTS) -c -o vehicle.o vehicle.c What does this rule signify?

<p>It compiles <code>vehicle.c</code> into <code>vehicle.o</code> if <code>vehicle.c</code>, <code>vehicle.h</code>, <code>surface.h</code>, <code>vec3.h</code>, or <code>image.h</code> have been modified since the last compilation. (A)</p> Signup and view all the answers

Assume a complex project where module_a.cpp includes header_x.h and module_b.cpp includes both header_x.h and header_y.h. Furthermore, header_x.h has a forward declaration of a class defined in header_y.h, but does not include header_y.h directly. If both module_a.o and module_b.o are linked into a final executable, and changes are made only to header_y.h, which of the following minimal set of recompilations and relinking is strictly necessary, assuming a standard make utility and properly configured Makefile?

<p><code>header_y.h</code>, <code>module_b.cpp</code>, <code>module_b.o</code>, and the final executable. (D)</p> Signup and view all the answers

What is a crucial distinction between a 'program file' and a 'process'?

<p>A program file is static content on disk, while a process is a running instance of that program in memory. (C)</p> Signup and view all the answers

Consider a scenario where a high-level language is compiled into bytecode, which is then executed on a virtual machine that employs Just-In-Time (JIT) compilation. If the underlying hardware architecture has a unique feature that can significantly optimize a particular algorithm, what would be the most effective approach to leverage this feature, assuming minimal changes to the original high-level code are desired?

<p>Optimize the virtual machine's JIT compiler to recognize and utilize the hardware feature during runtime compilation of the relevant bytecode. (C)</p> Signup and view all the answers

What is the purpose of the include_directories command in the CMakeLists.txt file?

<p>To add the specified directories to the compiler's include path, allowing it to find header files. (B)</p> Signup and view all the answers

What happens if the find_package(OpenGL REQUIRED) command fails to locate the OpenGL library?

<p>The CMake generation process aborts with an error. (B)</p> Signup and view all the answers

Which command is responsible for linking the OpenGL and GLUT libraries to the so_game executable?

<p>target_link_libraries(so_game ${OPENGL_gl_LIBRARY} ${GLUT_glut_LIBRARY} m) (C)</p> Signup and view all the answers

Examine the following CMake code snippet:

find_package(OpenGL REQUIRED) find_package(GLUT REQUIRED)

If the system has multiple versions of OpenGL installed, but the OpenGL_gl_LIBRARY variable points to an older version, and OpenGL_INCLUDE_DIRS points to the newest version, what is the most likely outcome during compilation or runtime?

<p>The program will compile, but may exhibit undefined behavior at runtime due to the version mismatch. (D)</p> Signup and view all the answers

Consider a scenario where the GLUT package is found, but the GLUT_glut_LIBRARY variable is not correctly set by find_package(GLUT REQUIRED). What is the most likely consequence when building the so_game executable?

<p>The linker will fail to find the <code>GLUT</code> library, resulting in unresolved symbols and a build error. (A)</p> Signup and view all the answers

Which of the following is NOT a typical dependency for a simulator?

<p>A physics engine for simulating gravitational forces. (B)</p> Signup and view all the answers

What are the two primary components of a C++ package from a build perspective?

<p>Include files (.h, .hpp) and library files (.so, .a). (A)</p> Signup and view all the answers

What is the purpose of the -I flag during the .o generation (compilation) step?

<p>To specify the directories where the compiler should search for include files. (A)</p> Signup and view all the answers

During the linking phase, what is the function of the -l flag?

<p>Links a specific library file (e.g., <code>-lm</code> links <code>libm.so</code>). (C)</p> Signup and view all the answers

What is the primary function of pkg-config?

<p>To automate the retrieval of compilation parameters (include paths, library paths and libraries themselves) for installed libraries and packages. (A)</p> Signup and view all the answers

Where does pkg-config obtain the information about installed packages?

<p>From *.pc files located in directories specified by the <code>$PKG_CONFIG_PATH</code> variable. (D)</p> Signup and view all the answers

Examine the following line from a makefile: LIBS=$(shell pkg-config --libs glut glu gl) -lm. What is being accomplished?

<p>The <code>LIBS</code> variable is being set to include the math library, in addition to the libraries required by the <code>glut</code>, <code>glu</code> and <code>gl</code> packages, as determined by <code>pkg-config</code>. (B)</p> Signup and view all the answers

What is the primary advantage of using CMake over traditional makefiles and pkg-config?

<p>CMake simplifies cross-compilation and handles differences in compiler options across different systems. It also requires you to write less in general. (A)</p> Signup and view all the answers

In a CMakeLists.txt file, what is the purpose of the find_package() command?

<p>It locates and configures external libraries or packages required by the project. (D)</p> Signup and view all the answers

Consider the following CMakeLists.txt snippet:

target_link_libraries(my_executable ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} m)

What is the significance of m in this context?

<p>It links the standard math library (<code>libm.so</code> or <code>libm.a</code>) to the executable. (A)</p> Signup and view all the answers

What does the target_link_libraries command do in a CMakeLists.txt file?

<p>It specifies the libraries needed to link against the target executable or library. (B)</p> Signup and view all the answers

What is the purpose of the add_subdirectory() command in CMake?

<p>It includes another CMakeLists.txt file from a specified directory. (D)</p> Signup and view all the answers

In CMake, how are variables expanded?

<p>Using curly braces with a dollar sign: <code>${}</code> (D)</p> Signup and view all the answers

If SHARED is omitted when using the add_library() command, what type of library is created?

<p>Static library. (A)</p> Signup and view all the answers

Given the statement set(SOURCES helpers.c image.c), how would you append main.c to the SOURCES variable in CMake?

<p><code>set(SOURCES ${SOURCES} main.c)</code> (A)</p> Signup and view all the answers

Assuming OpenGL and GLUT are correctly installed, what is the effect of the line include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})?

<p>It includes the header files from OpenGL and GLUT, making their functions available. (C)</p> Signup and view all the answers

Consider a project that uses so_game.c, graphics.c, and physics.c. The graphics.c file depends on shared_lib.c, which should be compiled into a shared library named shared. Which CMake code block correctly sets up the dependencies?

<p><code>add_library(shared SHARED shared_lib.c) add_executable(so_game so_game.c graphics.c physics.c) target_link_libraries(so_game shared)</code> (A)</p> Signup and view all the answers

Given the following CMake code: set(VARIABLE "one") set(VARIABLE "${VARIABLE} two") set(VARIABLE "${VARIABLE} three") What is the final value of the VARIABLE?

<p>&quot;one two three&quot; (B)</p> Signup and view all the answers

Flashcards

Program

Software executed by an interpreter, emulator, or directly by the CPU.

Interpreter

A program that executes a source file directly, parsing and executing code on the spot.

Bytecode

Machine language for a virtual processor that ensures portability between architectures.

Emulator

A program that emulates a virtual CPU to run bytecode.

Signup and view all the flashcards

Process

A running instance of a program, created when a program file is loaded into memory.

Signup and view all the flashcards

Makefile

A file containing instructions for the make utility to automate the compilation process.

Signup and view all the flashcards

make command

A command-line utility that reads a Makefile and automates the building of software.

Signup and view all the flashcards

Object dependant compilation

Object dependant compilation verifies which rules to execute based on the modification time.

Signup and view all the flashcards

touch command

This command updates the modification timestamp of a file.

Signup and view all the flashcards

External Packages

Indicates that the project relies on external libraries or other projects.

Signup and view all the flashcards

Header Files (.h, .hpp)

Files with declarations of functions, classes, and variables used in source files.

Signup and view all the flashcards

Source Files (.c, .cpp, .S)

Files containing the actual code implementation.

Signup and view all the flashcards

Object Files (.o)

Intermediate files produced after compiling source code, before linking.

Signup and view all the flashcards

Static Libraries (.a)

Collection of object files bundled into a single file for linking.

Signup and view all the flashcards

Shared Libraries (.so)

Libraries linked at runtime, allowing multiple programs to share the same library.

Signup and view all the flashcards

cmake_minimum_required

Specifies the minimum CMake version required to process the CMakeLists.txt file.

Signup and view all the flashcards

project()

Specifies the project name within the CMake build system.

Signup and view all the flashcards

find_package()

Finds and configures external libraries or packages for use in the project.

Signup and view all the flashcards

include_directories()

Adds specified directories to the compiler's include path.

Signup and view all the flashcards

add_executable()

Defines an executable target to be built from the specified source files.

Signup and view all the flashcards

target_link_libraries

Specifies external libraries needed for an executable to run.

Signup and view all the flashcards

CMake

A cross-platform build system generator.

Signup and view all the flashcards

add_library()

Adds a library target to be built. Can be static or shared.

Signup and view all the flashcards

Simulator Dependencies

A system relying on external tools like OpenGL, finite element systems, or linear algebra libraries to function.

Signup and view all the flashcards

C++ Package Components

Include files (.h, .hpp) and libraries (.so, .a) that provide pre-written code and functionalities.

Signup and view all the flashcards

Include Paths (-I)

Specifies where the compiler can find the include (.h) files needed for compilation.

Signup and view all the flashcards

Defines (-D)

Flags passed to the compiler that can conditionally enable or disable certain code sections in a library.

Signup and view all the flashcards

Library Directory (-L)

Specifies the directories where the linker can find the library files.

Signup and view all the flashcards

Linking Libraries (-l)

Specifies the libraries to link against during the linking phase. For example, -lm links to libm.so.

Signup and view all the flashcards

pkg-config

A tool that automates the retrieval of compilation parameters (includes and libraries) for installed packages.

Signup and view all the flashcards

*.pc Files

Text files containing the include (-I) and library (-l) files for each package. Found in $PKG_CONFIG_PATH.

Signup and view all the flashcards

Study Notes

  • A program is a piece of software that is executed.
  • Programs can be executed by an interpreter, an emulator (virtual machine), or directly by the CPU.

Machines: Native

  • CPUs process instructions sequentially.
  • Each instruction consists of one or more binary digits.
  • CPUs fetch instructions and operands from memory.
  • 1 to 1 correspondence exists between machine instructions and architecture-specific alphanumeric opcodes (Assembly language).

Executing Bytecode

  • Some compilers/languages are designed with portability.
  • Machine language is produced for a virtual processor.
  • Running binaries requires a native program that emulates the virtual CPU.
  • Native machine code can be generated more quickly for some code chunks with Just In Time compilation.

Running an Interpreter Script

  • Interpreters are programs that executes a source file directly.
  • Execution is done during parsing.
  • Examples of interpreters are BASIC, BASH, PYTHON, and Matlab/Octave

Program Files and Execution

  • A program file contains machine code and meta-information.
  • This generates a memory image suitable for CPU execution
  • OS/Environment support is needed to make a memory image of a program file.
  • A running program is a processes.
  • One program file can be loaded multiple times, resulting in different processes.

Building a Program File

  • Files and their extensions:
    • .h, .hpp: Headers
    • .c, .cpp, .S: Source Files
    • .o: object files
    • .a: static libraries
    • .so: shared libraries
    • Executables (no extensions on unix)
  • The process involves compiling and linking.

Building an Executable Program

  • Example: compiler+linker+assembler: g++/gcc.
  • To generate an object file from a cpp use: g++ <options> -c <source file>.
    • E.g.: hello_world.cpp → hello_world.o
  • To link files together and compile if needed, the command is g++ <options> -o <name> <files>.
    • Generates a program file called <name>.
  • Same machinery holds if the C compiler (gcc) is used

Running an Executable

  • The command shell invokes the OS to create a process/ load the image form the disk.
  • The shell also loads the necessary shared libraries.
  • Shared objects must be specified with the option -1 <library_name> during compilation.

Large Builds

  • Projects are split into many files.
  • Each file gets compiled into a .o file.
  • When sources/headers are changed, the affected parts get recompiled/linked.
  • Dependencies of .o: sources used to build it and included headers.

Show the Build by Hand

  • A script can be used.
  • Each time a file is changed the script needs to reissue the compilation from scratch.
  • A clean build of a large project takes time with this approach, making it unacceptable.
  • The following commands can be called at the terminal to compile files
gcc -Wall -03 -std=gnu99 -I/usr/include/GL -c -o vec3.o vec3.c
gcc -Wall -03 -std=gnu99 -I/usr/include/GL -c -o surface.o surface.c
gcc -Wall -03 -std=gnu99 -I/usr/include/GL -c -o image.o image.c
gcc -Wall -03 -std=gnu99 -I/usr/include/GL -c -o vehicle.o vehicle.c
gcc -Wall -03 -std=gnu99 -I/usr/include/GL -c -o world.o world.c
gcc -Wall -03 -std=gnu99 -I/usr/include/GL -c -o helpers.o helpers.c
gcc -Wall -03 -std=gnu99 -I/usr/include/GL -c -o so_game.o so_game.c
gcc -Wall -03 -std=gnu99 -I/usr/include/GL -o so_game vec3.o surface.o image.o vehicle.o world.o
helpers.o so_game.o -lglut -1GLU -1GL -lm

Makefiles

  • Way to automate the build process.
  • Triggered by the modification time.
  • Rule form
    • <target> : [tab] <dependancies>
    • <tab> command
  • For example, to execut gcb if vec3.o is older the vec3.c or vec3.h:
    • vec3.0 : vec3.c vec3.h
    • gcc -std=gnu99 -c -o vec3.o vec3.c

Dependencies

  • Command: gcc (or g++) -MM <source file>. Gets the "head" of the rule (target and dependencies). E.g., $> gcc -MM vec3.c

  • The following is an example makefile

#var declarations, expanded with $(name)
CC=gcc
LIBS=-lglut -1GLU -1GL -lm
INCLUDES=-I/usr/include/GL
CCOPTS= -Wall -03 -std=gnu99 $(INCLUDES)
 
#1st rule <head>: <dependancies>, root of tree
all:  so_game

#to make vec.o you need vec.h and vec.c, bottom line
## is the command to execute if the rule matches
## <target> : <dependancies>
##  <command> 
vec3.o:  vec3.c vec3.h
  gcc $(CCOPTS) -c -o vec3.o vec3.c

surface.o:  surface.c surface.h vec3.h image.h
  gcc $(CCOPTS) -c -o surface.o surface.c
 
image.o:  image.c image.h
  gcc $(CCOPTS) -c -o image.o image.c
 
vehicle.o: vehicle.c vehicle.h surface.h vec3.h image.h
  gcc $(CCOPTS) -c -o vehicle.o vehicle.c
 
world.o: world.c world.h image.h surface.h\ vec3.h vehicle.h helpers.h
  gcc $(CCOPTS) -c -o world.o world.c
 
helpers.o: helpers.c helpers.h
  gcc $(CCOPTS) -c -o helpers.o helpers.c
 
so_game.o:  so_game.c world.h image.h surface.h\
  vec3.h vehicle.h helpers.h
  gcc $(CCOPTS) -c -o so_game.o so_game.c
  
## here we call the linker
so_game: vec3.o surface.o image.o vehicle.o \
  world.o helpers.o so_game.o
  gcc $(CCOPTS) -o so_game vec3.o surface.o \
  image.o vehicle.o worl.o helpers.o \
  so_game.o $(LIBS)

clean:
  rm -rf *.o *~ cube main so_game
  • Call by issuing $make in a folder containing a Makefile.
  • Triggers an object dependent compilation, that verifies rules to execute based on the modification time.
  • Update the change time with $ touch vec3.h and check waht happens with $make.

Dealing with External Packages

  • Complex projects are typically reliant on complex projects and or libraries

  • A simulator might rely on:

    • OpenGL system to display data
    • Finite element sim to simulate dynamics
    • Linear algebra libraries to calculate geometric calculation
    • Tools to to load/save formatted files and images
  • Finding packages might be annoying, due to

    • Different OS/distributions
    • Different revisions
    • Different architectures
    • Different revisions of a package being installed
    • Packages can depend on each other
  • To the extent of a C++ build, a package comes as:

    • A set of include files (.h, .hpp).
    • A set of libraries (.so, .a).
  • The following aspects need to be specified in the o generation:

    • Include pathes which contain the .h files of he project.
      • -I<path1> -I<path2>
    • Potential defines that might trigger library specific behaviors
      • -D<statement> (Equivalent to #define <statement> in the source file!)
  • The following aspects need to be specified in the linking phase:

    • The directory of the libraries
      • -L <path>
    • The libraries
      • -l<libname_without_prefix>, e.g. -lm links libm.so’

pkg-config

  • How to automate retrieval of compilation parameters for installed libraries and packages.
  • You can either hardcode the locations of the library files and includes or automate the search to find those locations.
  • Database with text files is used to describe packages. Provide for each package the include path and the library path.
  • Usage Example:
$pkg-config --libs gl
$pkg-config --cflags gl
  • *.pc files contained in $PKG_CONFIG_PATH variable
  • Contains answer to potentional queries
CC=gcc # CC is our compiler, we can change later if we want

## execute pkg-config to find the libraries
LIBS=$(shell pkg-config --libs glut glu gl) -lm
INCLUDES=$(shell pkg-config --cflags glut glu gl)
 
CCOPTS= -Wall -03 -std=gnu99 $(INCLUDES)
OBJS=vec3.o surface.o image.o vehicle.o wolrd.o helpers.o so_game.o
 
.phony: clean all
 
all: $(OBJS)
  $(CC) $(CCOPTS) -o so_game $^ $(LIBS)
 # generic rule, to make a .o you need a .c and a .h of the same name
 %.o:%.c %.h
  $(CC) $(CCOPTS) -c -o $@ $<
clean:
  rm -rf *.o *~ cube main so_game

CMake

  • CMake: a makefile generator, that requires to write less things.

  • Dependencies are deduced from sources (by using a compiler toolchain).

  • Only tell in CMakeLists.txt what packages are needed and where the sources can be found.

  • The following needs to be defined for each target

    • The files it requires.
    • What libraries it depends on.
  • Compilation involves two command calls

    • $cmake -c <path to CMakeLists.txt>
    • make to be called from same folder

CMake example

cmake_minimum_required (VERSION 2.8.11)
project (so_game)
 
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
 
include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS}) 
add_executable(so_game
  helpers.c
  image.c  so_game.c
  surface.c  vec3.c  vehicle.c  world.c
)
#tell the executable the libraries it needs
target_link_libraries(so_game
  ${OPENGL_gl_LIBRARY}  #
  ${OPENGL_glu_LIBRARY}
  ${GLUT_glut_LIBRARY}
  m
)
  • add_executable (<name> <files...>) Adds an executable with the specified name. add_library (<name> [SHARED] <files...>): adds a library if SHARED is omitted the library is static
  • To specify link dependencies, use target_link_libraries (target_name <libraries...>)
    • where are the traget names of teh libraries

Syntax

  • CMake relies on variables that can be lists or atoms, these variables are global.
  • Variables can be assigned with
    • `set ( value) // plain var
    • set (<variable_name> <value1> <value2> ... <value N>) // list
    • set (<variable_name> ${<variable_name>} <value>) // appends value to a list
  • Conditional contructucts such as if/if-else are supported and enclose construcuts.
  • Projects with nested folder have CMake uses add_subdirectory(<folder_name>) -In nested folders you can put an additional CMakeLists.txt

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Description

This lesson covers Makefiles, their role in automating compilation, and the commands used. It also explains dependency tracking, recompilation processes, and C++ header files. Key concepts include object file creation and software requirements.

More Like This

Makefile Basics Quiz
33 questions

Makefile Basics Quiz

ComfortableBowenite2676 avatar
ComfortableBowenite2676
C++ Compilation and Build Process
5 questions

C++ Compilation and Build Process

IrreproachableHeliotrope3605 avatar
IrreproachableHeliotrope3605
Use Quizgecko on...
Browser
Browser