Skip to content

Latest commit

 

History

History
155 lines (135 loc) · 4.7 KB

build_systems.md

File metadata and controls

155 lines (135 loc) · 4.7 KB
marp math theme paginate footer
true
katex
custom-theme
true

Build systems

Today:

  • Intro to build systems
  • Build commands in a script
  • Build commands in Makefile

📺 Watch the related YouTube video!


Special symbols used in slides

  • 🎨 - Style recommendation
  • 🎓 - Software design recommendation
  • 😱 - Not a good practice! Avoid in real life!
  • ✅ - Good practice!
  • ❌ - Whatever is marked with this is wrong
  • 🚨 - Alert! Important information!
  • 💡 - Hint or a useful exercise
  • 🔼1️⃣7️⃣ - Holds for this version of C++(here, 17) and above
  • 🔽1️⃣1️⃣ - Holds for versions until this one C++(here, 11)

Style (🎨) and software design (🎓) recommendations mostly come from Google Style Sheet and the CppCoreGuidelines


Let's set up our C++ files (again)

blah.h

#pragma once
namespace tools {
void PrintHello();
} // namespace tools

blah.cpp

#include <blah.h>
#include <iostream>
namespace tools {
void PrintHello() { std::cout << "Hello!" << std::endl; }
} // namespace tools

main.cpp

#include <blah.h>
int main() {
  tools::PrintHello();
  return 0;
}

It's annoying to compile libraries by hand, isn't it?

  • There are many commands we have to type in the right order
  • If we mess up their order --- the libraries don't compile!
  • We can definitely do better!
  • 🤔 Naive solution: we could write a bash script! build.sh
    # Building an executable from main.cpp using library blah.cpp
    c++ -std=c++17 -c blah.cpp -I . -o blah.o
    ar rcs libblah.a blah.o
    c++ -std=c++17 main.cpp -L . -I . -lblah -o main
  • Now imagine building many libraries and executables or only rebuilding when the code changes
  • We have to write lots of bash code to support this
  • To avoid that, people have written multiple build systems!

Make - a basic build system

  • Stores the commands in a Makefile file
  • Commands are executed as a chain of dependencies
  • 🚨 Use tabs () not spaces () to indent code in this file!
  • Has block structure and simple syntax (for simple cases 😉)
    target: dependencies
      command
      command
      command
  • Run make target from a folder with Makefile
  • Can have many targets with many dependencies
  • If target file exists and is not older than its dependencies --- nothing happens!
  • 💡 This allows us to avoid doing the work twice!
  • 💡 A very good make tutorial is here

A Makefile to build C++ code

all: main

main: main.cpp libblah.a
	c++ -std=c++17 main.cpp -I . -L . -lblah -o main

libblah.a: blah.o
	ar rcs libblah.a blah.o

blah.o: blah.cpp
	c++ -std=c++17 -c blah.cpp -I . -o blah.o

clean:
	rm -f blah.o libblah.a main
  • Run this with make from the same folder
  • 💡 Follow the order of operations here! What happens when?
  • 💡 Try to run make again! What happens now?
  • 💡 Change a file and re-run make again! Did it re-build?
  • 💡 Run make clean to remove the generated files

Problems with Makefiles 🤔

  • Makefiles are very powerful but can get pretty complex
  • The syntax is finicky and a bit too detailed, not really abstract
  • Writing a Makefile for a big project can be hard
  • Supporting different configurations also gets complex

Alternatives?

  • Use a different build system, e.g. ninja, bazel etc.
  • And/or use a build generator, like CMake, Meson, etc.

Stay tuned!


bg