Compiling Code using
Makefiles and GNUstep
2501ICT
Nathan
Makefiles
• Save compile time
• only recompile what’s necessary
• Help avoiding mistakes
• that would otherwise occur if outdated modules
were linked together
• Are language independent
• Objective-C
• C, C++, Java
P3 Lecture Copyright © 2002-2007 René Hexel. 2
February 2007 All rights reserved.
How do Makefiles work?
• Dependency Trees
• Targets
• Sources
• Target
• The module to be built
• Sources
• The source code for the Target
• Other targets that need to be build first
P3 Lecture Copyright © 2002-2007 René Hexel. 3
February 2007 All rights reserved.
Dependency Tree Example
Main_Mod
Module_A Module_B
Module_C Module_D
P3 Lecture Copyright © 2002-2007 René Hexel. 4
February 2007 All rights reserved.
Corresponding Makefile
Main_Mod: Module_A Module_B
Main
Module_A:
Module_B: Module_C Module_D
A B
Module_C:
C D
Module_D:
P3 Lecture Copyright © 2002-2007 René Hexel. 5
February 2007 All rights reserved.
Make Rules
• Lines directly below a Target
• No empty lines in between
• One or more empty lines after
• Indented by a TAB character
• Spaces won’t work!
• Shell commands to execute
• Compiler Calls
• Any other shell command
P3 Lecture Copyright © 2002-2007 René Hexel. 6
February 2007 All rights reserved.
Objective-C Example
main.m
file_a.m + .h file_b.m + .h
file_c.m + .h file_d.m + .h
P3 Lecture Copyright © 2002-2007 René Hexel. 7
February 2007 All rights reserved.
Corresponding Makefile
main.o: main.m a.h b.h
main.m
a.o: a.m a.h
a.m b.m b.o: b.m b.h c.h d.h
a.h b.h
c.o: c.m c.h
c.m d.m
c.h d.h d.o: d.m d.h
P3 Lecture Copyright © 2002-2007 René Hexel. 8
February 2007 All rights reserved.
Adding Make Rules
main.o: main.m a.h b.h
gcc -Wno-import -Wall -c -o main.o main.m
a.o: a.m a.h
gcc -Wno-import -Wall -c -o a.o a.m
b.o: b.m b.h c.h d.h
gcc -Wno-import -Wall -c -o b.o b.m
c.o: c.m c.h
gcc -Wno-import -Wall -c -o c.o c.m
d.o: d.m d.h
gcc -Wno-import -Wall -c -o d.o d.m
P3 Lecture Copyright © 2002-2007 René Hexel. 9
February 2007 All rights reserved.
Generic Rules
• Save a lot of typing
• Avoid repeated compiler calls (previous slide)
• Help with consistency
• Avoid repeating the same change all over the
place
• Simply list suffixes, e.g.
• .m.o:
• convert a ‘.m’ file to a ‘.o’ file
P3 Lecture Copyright © 2002-2007 René Hexel. 10
February 2007 All rights reserved.
Adding a Generic Rule
.SUFFIXES: .o .m .h
.m.o:
gcc -Wno-import -Wall -c -o $*.o $*.m
main.o: main.m a.h b.h
a.o: a.m a.h
b.o: b.m b.h c.h d.h
c.o: c.m c.h
d.o: d.m d.h
P3 Lecture Copyright © 2002-2007 René Hexel. 11
February 2007 All rights reserved.
Make Variables
• Allow more flexible make files
• Assigning a value:
CC=gcc
• Using a variable
• Use $(variablename)
• E.g., to compile a program, instead of gcc:
$(CC) -Wall …
P3 Lecture Copyright © 2002-2007 René Hexel. 12
February 2007 All rights reserved.
Linking it all together
.SUFFIXES: .o .m .h
.m.o:
$(CC) -Wno-import -Wall -c -o $*.o $*.m
main: main.o a.o b.o c.o d.o
$(CC) -o main main.o a.o b.o c.o d.o -lobjc
main.o: main.m a.o b.o
a.o: a.m
b.o: b.m c.o d.o
c.o: c.m
… P3 Lecture Copyright © 2002-2007 René Hexel. 13
February 2007 All rights reserved.
GNUstep Makefiles
• Have all the rules already pre-defined
• Only require some variables to be set
• Name of the Program
• Name of the individual Classes (.m and .h files)
• Flags to be used
• -Wall -Wno-import
• Include files for different projects
• Command line tools, GUI applications, …
• Should be named GNUmakefile
• or GNUmakefile.classname for testing classes
P3 Lecture Copyright © 2002-2007 René Hexel. 14
February 2007 All rights reserved.
Command Line Tool Example
# Include the common variables defined by the Makefile Package
include $(GNUSTEP_MAKEFILES)/common.make
# Build a simple Objective-C program, called Example
TOOL_NAME = Example
# The Objective-C Implementation files to compile
Example_OBJC_FILES = Main.m Some_Class.m Other_Class.m
# Class Header (Interface) files
Example_HEADER_FILES = Some_Class.h Other_Class.h
# Define the compiler flags
ADDITIONAL_CPPFLAGS = -Wall -Wno-import
# Include the rules for making Objective-C command line tools
include $(GNUSTEP_MAKEFILES)/tool.make
P3 Lecture Copyright © 2002-2007 René Hexel. 15
February 2007 All rights reserved.
After removing the comments
include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME = Example
Example_OBJC_FILES = Main.m Some_Class.m Other_Class.m
Example_HEADER_FILES = Some_Class.h Other_Class.h
ADDITIONAL_CPPFLAGS = -Wall -Wno-import
include $(GNUSTEP_MAKEFILES)/tool.make
P3 Lecture Copyright © 2002-2007 René Hexel. 16
February 2007 All rights reserved.
Application Example
include $(GNUSTEP_MAKEFILES)/common.make
APP_NAME = Example
Example_OBJC_FILES = Main.m Some_Class.m Other_Class.m
Example_HEADER_FILES = Some_Class.h Other_Class.h
ADDITIONAL_CPPFLAGS = -Wall -Wno-import
include $(GNUSTEP_MAKEFILES)/application.make
P3 Lecture Copyright © 2002-2007 René Hexel. 17
February 2007 All rights reserved.
Adding an external GUI
include $(GNUSTEP_MAKEFILES)/common.make
APP_NAME = Example
Example_OBJC_FILES = Main.m Some_Class.m Other_Class.m
Example_HEADER_FILES = Some_Class.h Other_Class.h
Example_RESOURCE_FILES = Example.gorm
Example_MAIN_MODEL_FILE = Example.gorm
ADDITIONAL_CPPFLAGS = -Wall -Wno-import
include $(GNUSTEP_MAKEFILES)/application.make
P3 Lecture Copyright © 2002-2007 René Hexel. 18
February 2007 All rights reserved.
Gorm
• GNU Object Relationship Manager
• Create a GUI (View) independent of Program
(Model).
• Model-View-Controller (MVC)
Paradigm
• A Controller connects the View with the
underlying Model.
• Changes to the GUI are independent from
changes to the model classes.
P3 Lecture Copyright © 2002-2007 René Hexel. 19
February 2007 All rights reserved.
Connecting Model and View
• Reference Controller from View
• Create a Controller class in Gorm
• Add outlets (object references) for all GUI elements
• Add actions for buttons, menus, etc.
• Controller instantiates Model classes
• Application just calls NSApplicationMain()
• Completely dynamic
• Changed GUI: no need to re-compile
• Allows different GUIs, languages, etc.
P3 Lecture Copyright © 2002-2007 René Hexel. 20
February 2007 All rights reserved.
Documentation
• Directly from source code
• autogsdoc
• extracts comments starting with /**
• GNUmakefile
• documentation.make: rules for invoking
autogsdoc
• DOCUMENT_NAME
• Sets the name of the documentation
• Document_AGSDOC_FILES
• Lists the files to scan for autogsdoc comments
P3 Lecture Copyright © 2002-2007 René Hexel. 21
February 2007 All rights reserved.
Tool+Documentation Makefile
include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME = Example
Example_OBJC_FILES = Main.m Some_Class.m Other_Class.m
Example_HEADER_FILES = Some_Class.h Other_Class.h
DOCUMENT_NAME = Documentation
Documentation_AGSDOC_FILES = Some_Class.h Other_Class.m
ADDITIONAL_CPPFLAGS = -Wall -Wno-import
include $(GNUSTEP_MAKEFILES)/tool.make
include $(GNUSTEP_MAKEFILES)/documentation.make
P3 Lecture Copyright © 2002-2007 René Hexel. 22
February 2007 All rights reserved.
GUI Application+Documentation
include $(GNUSTEP_MAKEFILES)/common.make
APP_NAME = Example
Example_OBJC_FILES = Main.m Some_Class.m Other_Class.m
Example_HEADER_FILES = Some_Class.h Other_Class.h
Example_RESOURCE_FILES = Example.gorm
Example_MAIN_MODEL_FILE = Example.gorm
DOCUMENT_NAME = Documentation
Documentation_AGSDOC_FILES = Some_Class.h Other_Class.m
ADDITIONAL_CPPFLAGS = -Wall -Wno-import
include $(GNUSTEP_MAKEFILES)/application.make
include $(GNUSTEP_MAKEFILES)/documentation.make
P3 Lecture Copyright © 2002-2007 René Hexel. 23
February 2007 All rights reserved.
Using dwarf
• ssh dwarf.cit.griffith.edu.au
• Use gcc to compile
gcc -Wall -Werror -Wno-import -o file file.c
• Use man to look up C functions
man fgetc
man 3 printf
man 2 open
man man
P3 Lecture Copyright © 2002-2007 René Hexel. 24
February 2007 All rights reserved.
Online References
• Writing GNUstep Makefiles
• P3 Modules Page
• Week 2 Reading Material
• GNUstep Makefile Package Docs
• P3 Resources Page – Web Links
• Gorm tutorial:
http://www.gnustep.it/pierre-
yves/index.html
P3 Lecture Copyright © 2002-2007 René Hexel. 25
February 2007 All rights reserved.