6

I am getting an error when running the following makefile with make -f makefile2 install (apart install the rest is working):

all:myapp

#which compiler
CC = gcc

#Where to install
INSTDIR = /usr/local/bin

#where are include files kept
INCLUDE = .

#Options for development
CFLAGS = -g -Wall -ansi

#Options for release
# CFLAGS = -O -Wall -ansi

myapp: main.o 2.o 3.o
    $(CC) -o myapp main.o 2.o 3.o

main.o: main.c a.h
    $(CC) -I$(INCLUDE) $(CFLAGS) -c main.c

2.o: 2.c a.h b.h
    $(CC) -I$(INCLUDE) $(CFLAGS) -c 2.c

3.o: 3.c b.h c.h
    $(CC) -I$(INCLUDE) $(CFLAGS) -c 3.c         

clean:
    -rm main.o 2.o 3.o

install: myapp
    @if [ -d $(INSTDIR) ]; \
      then \
      cp myapp $(INSTDIR);\
      chmod a+x $(INSTDIR)/myapp;\
      chmod og-w $(INSTDIR)/myapp;\
      echo "Installed in $(INSTDIR)";\
    else
      echo "Sorry, $(INSTDIR) does not exist";\
    fi

I'm getting the following error:

error /bin/sh: 7: Syntax error: end of file unexpected
make: *** [install] Error 2

From what I understand it is a white space/tabulation/non unix character problem in the last lines of the makefile (after install:). But even trying to delete all spaces and replacing with tabulation I didn't manage to run the makefile properly. The code comes directly from a programming book I'm reading and is an example. Any help appreciated!

2
  • Why not [ -d $(INSTDIR) ] || mkdir -p $(INSTDIR}? Most users would expect that behavior. Jan 29, 2013 at 0:32
  • Well, the code is only illustrating explanations about using shell in a makefile from the programming book, they probably just wanted to show this syntax, but thank you for the piece of advice.
    – Étienne
    Jan 29, 2013 at 0:36

1 Answer 1

12

You're missing a trailing slash on your else under the install rule. It should be:

install: myapp
    @if [ -d $(INSTDIR) ]; \
      then \
      cp myapp $(INSTDIR);\
      chmod a+x $(INSTDIR)/myapp;\
      chmod og-w $(INSTDIR)/myapp;\
      echo "Installed in $(INSTDIR)";\
    else\
      echo "Sorry, $(INSTDIR) does not exist";\
    fi
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.