Podcast
Questions and Answers
What is the primary purpose of a Makefile
?
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
?
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?
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
?
In the provided Makefile
snippet, what does the $(LIBS)
variable likely represent in the context of the final linking command for so_game
?
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.
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.
Which file extension typically denotes a header file in C++?
Which file extension typically denotes a header file in C++?
What is the primary requirement for a piece of software to 'happen' or execute?
What is the primary requirement for a piece of software to 'happen' or execute?
What is the primary function of the g++ -c
command?
What is the primary function of the g++ -c
command?
In a Makefile, what is the purpose of the $(name)
syntax?
In a Makefile, what is the purpose of the $(name)
syntax?
What is the role of an interpreter when running a script?
What is the role of an interpreter when running a script?
Which of the following is the most accurate description of 'Just In Time Compilation'?
Which of the following is the most accurate description of 'Just In Time Compilation'?
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?
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?
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?
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?
What is a crucial distinction between a 'program file' and a 'process'?
What is a crucial distinction between a 'program file' and a 'process'?
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?
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?
What is the purpose of the include_directories
command in the CMakeLists.txt file?
What is the purpose of the include_directories
command in the CMakeLists.txt file?
What happens if the find_package(OpenGL REQUIRED)
command fails to locate the OpenGL library?
What happens if the find_package(OpenGL REQUIRED)
command fails to locate the OpenGL library?
Which command is responsible for linking the OpenGL and GLUT libraries to the so_game
executable?
Which command is responsible for linking the OpenGL and GLUT libraries to the so_game
executable?
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?
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?
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?
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?
Which of the following is NOT a typical dependency for a simulator?
Which of the following is NOT a typical dependency for a simulator?
What are the two primary components of a C++ package from a build perspective?
What are the two primary components of a C++ package from a build perspective?
What is the purpose of the -I
flag during the .o
generation (compilation) step?
What is the purpose of the -I
flag during the .o
generation (compilation) step?
During the linking phase, what is the function of the -l
flag?
During the linking phase, what is the function of the -l
flag?
What is the primary function of pkg-config
?
What is the primary function of pkg-config
?
Where does pkg-config
obtain the information about installed packages?
Where does pkg-config
obtain the information about installed packages?
Examine the following line from a makefile
: LIBS=$(shell pkg-config --libs glut glu gl) -lm
. What is being accomplished?
Examine the following line from a makefile
: LIBS=$(shell pkg-config --libs glut glu gl) -lm
. What is being accomplished?
What is the primary advantage of using CMake over traditional makefiles and pkg-config?
What is the primary advantage of using CMake over traditional makefiles and pkg-config?
In a CMakeLists.txt file, what is the purpose of the find_package()
command?
In a CMakeLists.txt file, what is the purpose of the find_package()
command?
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?
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?
What does the target_link_libraries
command do in a CMakeLists.txt file?
What does the target_link_libraries
command do in a CMakeLists.txt file?
What is the purpose of the add_subdirectory()
command in CMake?
What is the purpose of the add_subdirectory()
command in CMake?
In CMake, how are variables expanded?
In CMake, how are variables expanded?
If SHARED
is omitted when using the add_library()
command, what type of library is created?
If SHARED
is omitted when using the add_library()
command, what type of library is created?
Given the statement set(SOURCES helpers.c image.c)
, how would you append main.c
to the SOURCES
variable in CMake?
Given the statement set(SOURCES helpers.c image.c)
, how would you append main.c
to the SOURCES
variable in CMake?
Assuming OpenGL and GLUT are correctly installed, what is the effect of the line include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
?
Assuming OpenGL and GLUT are correctly installed, what is the effect of the line include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
?
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?
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?
Given the following CMake code:
set(VARIABLE "one")
set(VARIABLE "${VARIABLE} two")
set(VARIABLE "${VARIABLE} three")
What is the final value of the VARIABLE
?
Given the following CMake code:
set(VARIABLE "one")
set(VARIABLE "${VARIABLE} two")
set(VARIABLE "${VARIABLE} three")
What is the final value of the VARIABLE
?
Flashcards
Program
Program
Software executed by an interpreter, emulator, or directly by the CPU.
Interpreter
Interpreter
A program that executes a source file directly, parsing and executing code on the spot.
Bytecode
Bytecode
Machine language for a virtual processor that ensures portability between architectures.
Emulator
Emulator
Signup and view all the flashcards
Process
Process
Signup and view all the flashcards
Makefile
Makefile
Signup and view all the flashcards
make
command
make
command
Signup and view all the flashcards
Object dependant compilation
Object dependant compilation
Signup and view all the flashcards
touch
command
touch
command
Signup and view all the flashcards
External Packages
External Packages
Signup and view all the flashcards
Header Files (.h, .hpp)
Header Files (.h, .hpp)
Signup and view all the flashcards
Source Files (.c, .cpp, .S)
Source Files (.c, .cpp, .S)
Signup and view all the flashcards
Object Files (.o)
Object Files (.o)
Signup and view all the flashcards
Static Libraries (.a)
Static Libraries (.a)
Signup and view all the flashcards
Shared Libraries (.so)
Shared Libraries (.so)
Signup and view all the flashcards
cmake_minimum_required
cmake_minimum_required
Signup and view all the flashcards
project()
project()
Signup and view all the flashcards
find_package()
find_package()
Signup and view all the flashcards
include_directories()
include_directories()
Signup and view all the flashcards
add_executable()
add_executable()
Signup and view all the flashcards
target_link_libraries
target_link_libraries
Signup and view all the flashcards
CMake
CMake
Signup and view all the flashcards
add_library()
add_library()
Signup and view all the flashcards
Simulator Dependencies
Simulator Dependencies
Signup and view all the flashcards
C++ Package Components
C++ Package Components
Signup and view all the flashcards
Include Paths (-I)
Include Paths (-I)
Signup and view all the flashcards
Defines (-D)
Defines (-D)
Signup and view all the flashcards
Library Directory (-L)
Library Directory (-L)
Signup and view all the flashcards
Linking Libraries (-l)
Linking Libraries (-l)
Signup and view all the flashcards
pkg-config
pkg-config
Signup and view all the flashcards
*.pc Files
*.pc Files
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
- E.g.:
- To link files together and compile if needed, the command is
g++ <options> -o <name> <files>
.- Generates a program file called
<name>
.
- Generates a program file called
- 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 aMakefile
. - 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
).
- A set of include files (
-
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!
)
- Include pathes which contain the
-
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’
- The directory of the libraries
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
- where
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
- `set (
- 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 additionalCMakeLists.txt
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
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.