Understanding makefiles in Linux

Posted on 2009-04-10
Last Modified: 2012-05-06

I'm new to Linux, I'm using FEDORA 10, for no reason at all, somebody recommended to me and that was it. The thing I'm curios about is when you build something using the make command.

I've attached some of the code of this makefile, and looking at it in essence what it's telling me it's that it'll installed in /usr/local/bin, the progs, that are two files, aiod and

The thing is that when you use the make command in the local directory where you have download the source files, it builds it there, i.e, if the directory that contains the source files is /home/johndoe/Desktop/aiod for instance it'll create them there. not in the /usr/local/bin that says in the makefile.

Also it generates some *.o, or object files that ,as well as the main program, builds them in the same directory.

the thing is that i don't know is what to do with these files, theses object ones. I have to move the program i've just build in the usr/local/bin otherwise i get "command not found", because it has to be in one of the directories of the $PATH, as for these object files (*.o) I copied them in /usr/local/lib, for a lack of a better place, because i thought this program might need them. I have not tested them throughly so i don't know which features will not work, and i don't know if it'll be due to this object files, so far the program have worked.

Where do i put these files, these object ones, and am i missing some parameter to the make command so it'll build the files in the directory specified in the makefile?


CC      = gcc
# CFLAGS  := -Wall -W -g -DPREP_BUFS
PROGS   = aiod
LDLIBS  = -laio -lrt -lm
OBJS    = aiod.o affinity.o args.o misc.o async_io.o lat.o sync_io.o
all: depend $(PROGS)
INSTALL	= install
prefix	= /usr/local
bindir	= $(prefix)/bin
install: $(PROGS)
	$(INSTALL) -m755 -d $(DESTDIR)$(bindir)
	$(INSTALL) $(PROGS) $(DESTDIR)$(bindir)
dist: aiod-0.9.tar.bz2
	-rm -f aiod-0.9.tar aiod-0.9.tar.bz2
	-git-archive --format=tar HEAD | bzip2 > aiod-0.9.tar.bz2
	@$(CC) -MM $(CFLAGS) -I.. *.c 1> .depend
clean: docsclean
	-rm -f *.o $(PROGS) .depend
	-rm -f cscope.out tags log
	-rm -f aiod-0.9.tar.bz2 aiod.tar.bz2
	-rm -f instrumentation/*.out
	$(MAKE) -C doc all
	$(MAKE) -C doc clean
aiod: $(OBJS)
	-cp $? $@
ifneq ($(wildcard .depend),)
include .depend

Open in new window

Question by:lguifarro

Author Comment

ID: 24120974

I've forgot to ask this too

There are some Header Files .h in the source files, I'm assuming that it needs them for the build when you call the make commando, but shouldn't i put these somewhere, like in the usr/nclude, or the /usr/incclude/linux directories????


LVL 14

Assisted Solution

small_student earned 100 total points
ID: 24121610
Hi, A couple of things to note here:

1- Ever thought of using yum instead of installing from source, its a life saver.

2- Since we are talking about installing from source:
a) First did you do a ./configure
This step configures your package for correct paths

b)You can delete all the .o files (object files), you can also do after you finish make install
make clean
This actually deletes the .o files

c)You can delete the source files all together after you finish

Best Regards
LVL 16

Accepted Solution

ai_ja_nai earned 300 total points
ID: 24122058
ok, here I explain you: makefile and make are a way to install programs by souce. Normalli, when you compile something you invoke the compiler (javac, cc, gcc, etcetcetc) on the source file and you get a binary object in output. There's no absolute theoretical reason why we shouldn't do everything that way. But there's a practical one: when a program is made of hundreds of object files, compiling everything and moving object code around your filesystem one by one by hand can be tedious, long and dangerous (in the way taht if you miss one file the whole thing won't run).

make is just a way to automate the task: you launch fist ./configure script, which checks if all the dependencies are met and if all the necessary software is present; all these checks and additional info on the system will be output in a file that make will look at during compiling. Then, you launch make; make will look for the configuration file and, most important, to the Makefile, which is just a cookbook for the compiler: it tells where are the source files, how to compile them and where to put them once the job is done.

Invoking make alone will just compile everything. To put everything in the right place, you will need to invoke 'make install', which is a funcion of make that will just copy compiled stuff in their place.
LVL 16

Assisted Solution

ai_ja_nai earned 300 total points
ID: 24122062
One more thing:

>c)You can delete the source files all together after you finish

This is an EXTREMELY WRONG thing to do, because if you delete the place where you compiled your program, you'll loose all the infos that will enable you to uninstall the program. 'make uninstall' is a command available for this purpose, and once you wipe away the information output by configure and the Makefile, the only way to remove your program will be by hand...

Assisted Solution

ewest02 earned 100 total points
ID: 24122526
Most software source packages include some documentation in the form of READMEs, INSTALL, etc text files. These will (hopefully) contain sufficient information about the build/make process for that package. There may or may not be a configure script -- and I am guessing from the contents of your posted makefile, one is not included with this source.

It is many times a Good Thing (tm)  to *test* your freshly built software before installing it. You can set the target installation directory by modifying the "prefix" value. This will vary in specifics from package to package. With a configure script, you can do this on the command line. In your case, just edit the makefile directly.

Suggestion: once you are satisfied with the newly minted binaries and they have been installed in their final resting places, run " make clean" .  If you still have the original source archive in a separate directory, delete the build/staging directory for the package. Otherwise recreate the archive for future reference. Note too, as per some makefiles (eg the one in this posting) there is no " make uninstall" target.

If you want the how' s, why' s and what for' s about GNU make, review the documentation from Paul Smith etal on the GNU make site  It is well written and maintained.



Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question