Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple makefile compilation question 1

Status
Not open for further replies.

jjohnn

Technical User
Feb 11, 2003
43
US
I am starting out in C (as well as Linux), and want to automatically use the -Wall and -pedantic in gcc, as I understand that can be useful. How do I do this more or less automatically? In other words, how can I not have to always type:

gcc -o hello -Wall -pedantic hello.c
 
A simple Makefile that looks like
[tt]all:
gcc -ohello -Wall -pendantic hello.c[/tt]
would allow you to execute [tt]make[/tt] and it would compile your program. //Daniel
 
try a makefile:
NOTA: <tab> is really a tab char!!

CCC = gcc
OPT = -Wall -pendantic
EXE = hallo abba navy # and more source files

all:<tab>$(EXE)

clean:
<tab>$(RM) $(EXE); make all

$(EXE):<tab>$$@.c
<tab>$(CCC) -o $@ $(OPS) $@.c
<tab>@/bin/touch $@
 
Thanks. What do

$@
$$@
$(OPS)

mean?

what is teh &quot;touch&quot; doing?

John
 

hi,
using a makefile is helpfull if you are working on a big project and files dependencies are huge . so that if you modify one file , you don't have to compile again the files dependent on it . makefile will automatically detect it .
in your case you can alias gcc as
alias gcc=&quot;gcc -pedantic -Wall&quot;
you can change it as per your need

For using makefile the syntax is

<output file> : <<tab>> <dependencies
<<tab>> <rule applying on dependencies >

you can use predefined macro in the rule

$@ --- for output file
$< --- for first dependency
$^ --- for all dependency

for user defined one
MYVAR=........

to access it you write
$(MYVAR) --- same as in shell

sample makefile
TARGET=a.out
DEPEND=a.c
GCC=gcc -Wall -pedantic

$(TARGET) : $(DEPEND)
$(GCC) -o $@ $^
expeands as
gcc -Wall -pedantic -o a.out a.c
for more help read info make

as far as touch is concerned it changes the timestamp of the file . it might be used if you want the makefile to explicitly make a file ,you change the timestamp of its dependencies . (simple make rule if timestamp of target is older than of dependencies execute the rules )

hope it will help you
bye

 
Thanks. Is the alias possible in Windows NT (which is where I do most of my work)? I looked in the help screen of teh command prompt window and the general NT help, but couldn't find anything.

John
 
I tried this:
TARGET=a.out
DEPEND=printf.c
GCC=gcc -Wall -pedantic

$(TARGET):$(DEPEND)
$(GCC) -o$@$^

and ran
make printf.c

my shell returned:
make: ***No rule to make target 'printf.c'. Stop.
shell returned 2

Any hints
 
Got it. For my source named &quot;squeeze2.c&quot;:

SRC=squeeze2.c
EXE=sq
RM=del


CC=gcc
CFLAGS= -ansi -Wall -pedantic
DEL= del
$(EXE): $(SRC)
$(CC) $(CFLAGS) $(SRC) -o $(EXE)


clean:
$(RM) $(EXE)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top