1 / 11

Automate Your Builds with Make: Understanding Makefiles and Compilation Processes

Make is a powerful utility that streamlines the compilation of your programs by automatically managing dependencies and recompilation. By utilizing a Makefile (or makefile) in your directory, Make defines the relationships between files, detailing how to create targets from sources. This guide walks you through a simple example and a more complex scenario involving multiple source files, headers, and targets, highlighting key aspects of writing effective Makefiles. Whether you’re developing small or extensive projects, Make simplifies your build process.

livia
Télécharger la présentation

Automate Your Builds with Make: Understanding Makefiles and Compilation Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Make • Make is a system utility that automatically compiles your programs for you • Make looks for a file named Makefile (or makefile) in the current directory • Describes the relationships among files that comprise you program • Gives the commands for updating each file • Using the Makefile, make automatically: • Determines which files need to be recompiled • Issues the commands to recompile them

  2. A Simple Program // hello.cpp #include <iostream> #include “hello.h” using namespace std; int main() { cout << MESSAGE << endl; }

  3. The Header File // hello.h #define MESSAGE "Hello World!"

  4. Compiling and Running the Program • Compiling: • g++ -O2 -o hello hello.cpp • Running • ./hello • Result: • Hello World!

  5. Makefile • Composed of: • Comments (from a pound sign to the end of a line) # This is a comment • Macros – just like in your program CFLAGS= -O2 • Targets – lists dependencies and rules for updating hello: hello

  6. The Makefile # Makefile for hello.cpp CFLAGS = -O2 hello: hello.h hello.cpp g++ $(CFLAGS) -o hello hello.cpp

  7. Compiling and Running the Program with Make • Compiling: • make • Running • ./hello • Result: • Hello World! • Note: make is really unnecessary for such a simple program, but is essential for programs with lots of source files

  8. Make - Pitfalls • Make is very picky about some things: • Indenting must be done with tabs not spaces • Dependencies must be correct • If nothing has changed make will tell you the target is “up to date”

  9. A More Complicated Example • 5 source files • Some shared header files • Multiple targets • Make creates the first target by default • E.g. make • You can specify others: • E,g, make clean

  10. Let’s Write a Makefile • 3 header files • src1.h – used by src1.cpp • src2.h – used by src2.cpp • global.h – used by src1.cpp, src2.cpp, and main.cpp • 4 source files • src1.cpp – routines used by main.cpp • src2.cpp – routines used by main.cpp • src3.cpp – routines used by src2.cpp • main.cpp – main program

  11. Questions • What will make do if we touch: • global.h? • src1.h? • src2.h? • src3.cpp? • main.cpp?

More Related