LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-10-2012, 06:40 AM   #1
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Rep: Reputation: Disabled
Question help about makefiles


hello everyone. i am a little new to linux environment. i am trying to make a makefile which has the following requirements. everything is going fine except the 3rd point. what command can i use for the 3rd point ?


Suppose there are two C source (transaction.c and reports.c) and two header files (trans.h and reps.h). Write a Makefile/makefile that reflects the following relationships:

1. The C source files transaction.c and reports.c are compiled to produce an executable accts.

2. transaction.c and reports.c include a header file accts.h
3. The header file accts.h is composed of two other header files: trans.h
and reps.h.
 
Old 10-10-2012, 06:53 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,509

Rep: Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434
would be better to show your makefile and we will try to fix...
 
Old 10-10-2012, 07:18 AM   #3
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Exclamation

here is the makefile i wrote. does it match the specification asked above ? i am confused about the 3rd point, where it says " the header file accts is composed of two other header files (trans.h and reps.h)". i doubt if it matches the requirement no.3

Quote:
OBJ = transaction.o reports.o

accts : $(OBJ)
gcc -o $@ $(OBJ)

transaction.o : transaction.c trans.h accts.h
gcc -c transaction.c

reports.o : reports.c reps.h accts.h
gcc -c reports.c

clean :
rm accts $(OBJ)
 
Old 10-10-2012, 07:27 AM   #4
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Question

i have posted the makefile as well. does anyone care to help me ?
 
Old 10-10-2012, 07:32 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,516

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
This ought to do it:
Code:
MAKEFILE =      Makefile

CFLAGS  =       -O2

MAINS   =       accts

OBJECTS =       reports.o transaction.o

SOURCES =       reports.c transaction.c

LCLINCS =       accts.h reps.h trans.h

ALL:            $(MAINS)

accts:          reports.o transaction.o
        $(CC) $(CFLAGS) -s -o accts reports.o transaction.o $(LDFLAGS)

reports.o:      $(LCLINCS)

transaction.o:  $(LCLINCS)

clean:
        rm -f $(OBJECTS)

clobber:
        rm -f $(OBJECTS) $(MAINS)
Notes:
  • Between the colon ( and anything on the same line is one or more tabs (not spaces).
  • The make utility "knows" about dependencies; i.e., a target (report.o) does not need explicit dependency on a C file (report.c) -- make know that -- but it does depend upon the header files.
You would simply type
Code:
make
to wind up with accts (which you could then copy to some bin directory).

You would type
Code:
make clean
to remove all the object files.

You would type
Code:
make clobber
to remove all the object and executable files (if you were going to place this source and Makefile in, say, CVS or some other source code control system).

Laying a Makefile out like this is a little easier to understand sometimes and later you can get "good" with the built-in macros.

Hope this helps some.
 
1 members found this post helpful.
Old 10-10-2012, 07:39 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 7,504

Rep: Reputation: 2388Reputation: 2388Reputation: 2388Reputation: 2388Reputation: 2388Reputation: 2388Reputation: 2388Reputation: 2388Reputation: 2388Reputation: 2388Reputation: 2388
IIRC, the customary "clean everything" target-name is distclean ... but of course, it's merely a custom.

see also automake.

Last edited by sundialsvcs; 10-10-2012 at 07:40 AM.
 
Old 10-10-2012, 07:57 AM   #7
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Question

Quote:
accts: reports.o transaction.o
$(CC) $(CFLAGS) -s -o accts reports.o transaction.o $(LDFLAGS)
the macro LDFLAGS and CC is not defined anywhere. doesn't they need to be defined ? could u please tell me what does -O2 and -s do ?
 
Old 10-10-2012, 08:19 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,509

Rep: Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434
-O2 sets the optimization level
-s strip
you can check the manuals about detailed info
$(LDFLAGS) is used to specify linker related flags
 
Old 10-10-2012, 08:28 AM   #9
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,516

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
I should have mentioned that I do Makefiles the "AT&T" way, not the "GNU" way. Because I work in both Linux and SPARC Solaris (which is SVR4 and, generally does not include gcc but rather the Sun C compiler), I follow the AT&T way which is fully compatible with GNU make; that is, a Makefile built for SVR4 make will work just fine with GNU make but not the other way around. gcc will generate Makefile dependencies (with the -M command line option), Sun C won't so I use an AT&T Toolchest utility called makefile which auto-generates a Makefile from a collection of C, shell, Lex, Yacc and other source code (but it uses nm, tsort and other utilities that are either different (nm) or simply not there in Linux -- tsort is one of those really handy things that I miss in Linux). I've found it useful to generate Makefiles on Solaris then just copy them to Linux which works just fine for me (YMMV) because of the lack of tsort in Linux (and I don't know if there's a similar utility, haven't looked that hard; tsort does topological sort).

The AT&T way was to use clean and clobber for, uh, cleaning up. As far as I know, distclean is used to completely wipe clean a configure, make, make install from Makefile(s) generated with autoconf and automake (pretty handy for big projects that can be downloaded and configured for whatever platform you're running on -- particularly the differences between Linux and Solaris). I've never mastered autoconf and automake, probably because I've already got a tool that works just fine for me.

Anyway, six of one, half dozen of the other or, perhaps, different stroke, eh?
 
Old 10-10-2012, 08:50 AM   #10
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,516

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
Quote:
Originally Posted by sam_sung View Post
the macro LDFLAGS and CC is not defined anywhere. doesn't they need to be defined ? could u please tell me what does -O2 and -s do ?
CC is the default C compiler on the system -- it's a built-in default in make and is gcc on a Linux system (it can also be cc which is a symbolic link to gcc on most if not all Linux systems).

LDFLAGS is commonly used to define libraries required by targets you're building; e.g.,
Code:
LDFLAGS = -lm
would define the math library to the linker. Note that it doesn't hurt to have one or more LDFLAGS defined that then apply to every target (if a particular target doesn't use anything in a given library it's more or less ignored). By using the -lsomething you're only extracting objects from a given library if needed, you're not linking the entire content of the library.

You can, of course, define multiple variants of LDFLAGS and edit their names into the compilation lines in your Makefile; you're using LDFLAGS as a convenient definition that affects all targets in one place. And, too, you can simply delete LDFLAGS from the lines that don't require any library flags or add additional libraries with an explicit -llibname as necessary. It's something you do once when you're constructing your Makefile.

Something else that useful is multiple CFLAGS definitions for multiple platforms (if you're working with multiple platforms, that is); e.g.,
Code:
#       use this one for gcc 32-bit
#CFLAGS =       -O2
#       use this one for Sun SPARC
#CFLAGS =       -xarch=native64 -xO3 -xautopar -xbuiltin=%all -I$(BASDIR)/include
#       use this one for 64-bit
CFLAGS  =       -O2 -FPIC -m64
You can simply uncomment the one you need or place the one you need last in the sequence -- make uses the last definition.

Hope this helps some.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Makefiles...... :( Idk blaaa Linux - Newbie 4 02-01-2010 08:27 PM
makefiles... DEF. Programming 2 04-20-2009 02:46 PM
Makefiles deveshs Linux - Software 2 05-02-2005 06:26 AM
Those makefiles... boku Programming 10 03-01-2005 09:19 AM
Makefiles and Me dpottinger Linux - Newbie 5 07-25-2004 12:14 AM


All times are GMT -5. The time now is 07:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration