Home Home > 2013 > 11 > 05 > openSUSE and GCC part 5: Make love me do
Sign up | Login

Deprecation notice: openSUSE Lizards user blog platform is deprecated, and will remain read only for the time being. Learn more...

openSUSE and GCC part 5: Make love me do

November 5th, 2013 by

In this point of time if you haven’t any idea what is pkg-config or GCC you should be reading this. Or if you still do please make sure you read about them from these blog posts. ‘Make‘ as a tool doesn’t do anything easier it just hides not-so-easy-part from eyes of public. ‘Make’-tool executes (in default) Makefile-script that should tell how to build your applications step by step. There is no guaranties that it’s easier to understand how it’s build or you to understand that after a while.

What should I make then?

As ‘make’ spreads wide area of operating system it’s not very new tool. You can use make in AIX, HP/UX, BSD variants, Linux, Windows or Solaris. Most of them they got their own make tool that got it’s own dialect. If you are not using AIX, HP-UX, Solaris or pure nmake from Visual Studio I recommend to learn GNU Make dialect. With that you can easily adapt those others. One can pretend that ‘make’ is nearly programming language for building application.
Mostly because ‘make’ ain’t cleanest way building applications there is also other tools as well: Python Scons, Apache Ant and CMake for example. They all are as powerful as ‘make’ if compare them as building tools. You got to remeber Ant is mostly for Java applications but you can use it for anything if you are willing and SCons is general building tool. One can argue all the day and night which of them is the best. One thing is correct ‘make’ is not the cleanest but is factory standard.

First forget everything

First thing I like to say about make is forget everything you have learned in your life. There is only one way on doing things in ‘make’ and it’s only open for those who are true believers. Now forget those two sentences and you are in correct state of taking example building script as it is. This particular builds last blogs ‘SDL_image’ example program. First we install make

zypper install make

then make script. Save this in sample directory/Folder as ‘SDL_DisplayBitmap.c’ named ‘Makefile’ (In this case capitals are needed). If you use pico/nano make sure intend tabs are tabs not spaces if they are converted make won’t work.

SRC := SDL_DisplayBitmap.c
CFLAGS = $(shell pkg-config --cflags SDL_image)
LIBS = $(shell pkg-config --libs SDL_image)

all: 
	@echo "Building with make is so fun"
	gcc -o SDL_DisplayBitmap $(SRC) $(CFLAGS) $(LIBS)

.PHONY: install clean

clean:
	rm -f SDL_DisplayBitmap

install:
	@echo "This just PHONY install target"

What does it mean?

Okay let’s go line by line

SRC := SDL_DisplayBitmap.c

Variable that contains ‘SDL_DisplayBitmap.c’. Why it got ‘:=’ and no ‘=’ that’s is up to you find out.

CFLAGS = $(shell pkg-config --cflags SDL_image)
LIBS = $(shell pkg-config --libs SDL_image)

This could be done in one variable but it’s easier to use in future if it’s divided in two. Tell to run ‘pkg-config’-tool and pass what ever is output to variable ‘CFLAGS’ or ‘LIBS’. So they now have correct building flags and libraries.

all: 
<TAB>@echo "Building with make is so fun"
<TAB>gcc -o SDL_DisplayBitmap $(SRC) $(CFLAGS) $(LIBS)

Okay this is the main thing of ‘all:’-target. Traditionally there is ‘all’-target in ‘Makefile’ to build applications from ground up.
Now be attended! Always use intending tabs before commands in ‘Makefile’ no spaces. Okay? If ‘make’-tool says you are using spaces to intend convert them to tabs. ‘@’-mark is just don’t show what we are doing only print what is outputted to stdout and after that we compile using SRC, CFLAGS and LIBS variables. If you are using variable in Makefile it have to be ‘$(VARIABLENAME)’ not ‘$VARIABLENAME’ or ‘VARIABLENAME’. You can also use lowercase names but again tradition and habits comes in a way.

.PHONY: install clean

clean:
<TAB>rm -f SDL_DisplayBitmap

install:
<TAB>@echo "This just PHONY install target"

Normally there is clean and install targets available in every ‘Makefile’. It’s more easier your life stuff than must. I recommend to create them! It will not lead you to hate or suffering. These targets are easy to explain ‘clean’-target should get you source tree in state that if was before building and ‘install’-target should install your binary/binaries. Ahh.. and then there is ‘.PHONY’ target is you really are interested read all about it from docs.

How to make application

Copy Makefile to folder/directory you have ‘SDL_DisplayBitmap.c’ and run

make

after that you should have binary ‘SDL_DisplayBitmap’. if you want to clean your source just run

make clean

This was just hands dirty example. Make is so much more and more complicated you ever can imagine. Next we jump into Autotools for couple of blogs as they use make as tools to build applications.

Both comments and pings are currently closed.

Comments are closed.