Unix Shell - sh and Makefile

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > sh and Makefile





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author sh and Makefile
Mark

2005-02-25, 8:47 pm

Hello,

I have an unusual scenario for you. I am attempting to automate the
generation of some dependancies in make. I have written the following
code inside my makefile but am getting errors. I'm afraid I'm a newbie
to all this.

Could someone please help!

-------------------------------Makefile---------------------------------------
SHELL = /bin/sh
CXX = g++
CXXFLAGS = -g -Wall
DEFS = -D_MOSIX

# Automatically generate dependencies and include them in Makefile
depend: $(SOURCES) $(HEADERS)
for file in `find . -name *.cpp`; do \
filewosuffix=`echo "$$file" | sed 's/.cpp//g'` \
g++ $$file -MT '${OBJDIR}/"$$filewosuffix".o' >> depend \
echo '$${CXX} -c $${CXXFLAGS} $${DEFS} -o $$@ $$<' >> depend \
done
-------------------------------/Makefile---------------------------------------

I am getting the following errors:
/bin/sh: -c: line 2: syntax error: unexpected end of file

Thanks for your help.

Bill Seivert

2005-02-26, 2:50 am



Mark wrote:
> Hello,
>
> I have an unusual scenario for you. I am attempting to automate the
> generation of some dependancies in make. I have written the following
> code inside my makefile but am getting errors. I'm afraid I'm a newbie
> to all this.
>
> Could someone please help!
>
> -------------------------------Makefile---------------------------------------
> SHELL = /bin/sh
> CXX = g++
> CXXFLAGS = -g -Wall
> DEFS = -D_MOSIX
>
> # Automatically generate dependencies and include them in Makefile
> depend: $(SOURCES) $(HEADERS)
> for file in `find . -name *.cpp`; do \
> filewosuffix=`echo "$$file" | sed 's/.cpp//g'` \
> g++ $$file -MT '${OBJDIR}/"$$filewosuffix".o' >> depend \
> echo '$${CXX} -c $${CXXFLAGS} $${DEFS} -o $$@ $$<' >> depend \
> done
> -------------------------------/Makefile---------------------------------------
>
> I am getting the following errors:
> /bin/sh: -c: line 2: syntax error: unexpected end of file
>
> Thanks for your help.
>


You probably want to erase depend since you are only
appending to it. You need more semicolons and use double
quotes around the *.cpp in the find command and on the final
echo. One semicolon after each statement:

depend: $(SOURCES) $(HEADERS)
cat </dev/null >depend ; \
for file in `find . -name "*.cpp"`; do \
filewosuffix=`echo "$$file" | sed 's/.cpp//g'` ; \
g++ $$file -MT '${OBJDIR}/"$$filewosuffix".o' >> depend ; \
echo "$${CXX} -c $${CXXFLAGS} $${DEFS} -o $$@ $$<" >> depend ; \
done

(untested)

Bill Seivert

Markus Dehmann

2005-02-26, 2:50 am

Mark wrote:

> I have an unusual scenario for you. I am attempting to automate the
> generation of some dependancies in make.


That's so not unusual!

> I have written the following
> code inside my makefile but am getting errors. I'm afraid I'm a newbie
> to all this.

[code omitted]

There is a better way to do it:

SRC_DIR = src
OBJ_DIR = obj
SRC = $(SRC_DIR)/file1.cpp $(SRC_DIR)/file2.cpp
OBJ = $(SRCSRC_DIR)/%.cpp=$(OBJ_DIR)/%.o) # will be obj/file1.o ...
$(OBJ_DIR)/%.o: (SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<
Makefile.dep: $(SRC)
$(CXX) -I$(INCLUDES) -E -MM $^ 2>/dev/null | \
perl -pe 's!^([^:]+!$(OBJ_DIR)/$$1!' > $@
-include Makefile.dep # the dash is not a typo!

That writes all dependencies into Makefile.dep and includes it. All object
files get written into obj/ and don't clutter your src/ directory! (let's
add some keywords for google, since I looked for this once and didn't find
anything usefule: separate source tree from object tree, g++, compilation).

The dash before include is necessary, otherwise make complains that it
cannot find Makefile.dep -- it has to generate it first, using the
Makefile.dep rule!

Instead of $(CXX) -E -MM you could just use cpp -MM. You could also look at
the makedepend program, but I don't like the fact that it writes everything
into the Makefile directly.

Markus


Mark

2005-02-26, 2:50 am

Hey guys,

Thank you both!

Markus, you read my mind in what I was trying to do. And you're right
there wasn't a lot of good documentation out on the internet for it. I
hope this helps others as well.

I had looked into makedepend, but didn't like how fine-tuned it was. I
thought time would be bettter spent developing my scripting skills.

Kind Regards,
Mark

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com