Download presentation
Presentation is loading. Please wait.
1
Makefiles CSSE 332 Operating Systems
Rose-Hulman Institute of Technology 1
2
Announcements We will learn about Makefiles today
We will review some of the code today 2
3
Creating an executable
Compiler stage: All C language code in the .c file is converted into a lower-level language called Assembly language; making .s files. (can use -S option) Assembler stage: The assembly language code made by the previous stage is then converted into object code which are fragments of code that the computer understands directly. An object code file ends with .o. (Can use –c option) The object code is generated using the instruction set architecture of the underlying machine. Linker stage: The final stage in compiling a program involves linking the object code to code libraries which contain certain "built-in" functions, such as printf. This stage produces an executable program, which is named a.out by default. (Can use –o option to rename executable) Source: 3
4
To compile a program gcc main.c mypgm.c –o run or
gcc -c main.c -o main.o gcc -c mypgm.c -o mypgm.o gcc mypgm.o main.o -o run Important gcc flags: Can also use a Makefile A Makefile example Checkout the maketutorial project to your csse332 directory and study the Makefile 4
5
Makefiles Tutorial adapted from http://www.eng.hawaii.edu/Tutor/Make/
5
6
Compiling a single program
7
Compiling multiple related files
gcc green.c blue.c OR gcc green.c blue.c –o program1
8
More efficient compilation
gcc –c green.c => create green.o gcc –c blue.c => create blue.o gcc green.o blue.o => link and create a.out The “c” flag compiles and creates object files while ignoring functions and variables defined externally. The “c” flag does not link. 8
9
Dependency graphs and Makefiles
Target: Dependencies <tab> How to compile
10
Makefile all: project1 10
11
Using a Makefile Create a file called Makefile or makefile.
At the prompt: >>> make
12
Make clean clean: <tab>rm –f *.o <executables>
At the prompt: >>> make clean
13
To create multiple executables
Include the following line in your Makefile all: <exe1> <exe2> …. Write the rest of the Makefile At the prompt: >>> make
14
To make more than one exe
If you don’t want to regenerate all the executables every time Specify only the ones that should be generated with “all” OR Don’t use “all”; instead, type: >>> make <exe1> <exe2> ….
15
Macros in Makefiles CC = gcc DEBUG = -g LFLAGS = -Wall $(DEBUG)
CFLAGS = -Wall –c $(DEBUG) main: green.o blue.o $(CC) $(LFLAGS) green.o blue.o –o main green.o: green.c common.h $(CC) $(CFLAGS) green.c blue.o: blue.c common.h $(CC) $(CFLAGS) blue.c
16
References
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.