LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   make install error with Makefile I created (https://www.linuxquestions.org/questions/linux-newbie-8/make-install-error-with-makefile-i-created-884299/)

spiros 06-03-2011 05:48 AM

make install error with Makefile I created
 
hi everybody,

I am new to Linux and I am trying to install a C program from source using Makefile. So I 've created a "hello world" program and then a Makefile to install it. Then I logged in as root and used
make install

The following error occurs:
make: execvp: /usr/bin: Permission denied
make: *** [install] Error 127

here is the Makefile:

CC=gcc
CFLAGS=-c -Wall
PROGS = hello
INSTDIR = $(prefix)/usr/bin/
INSTMODE = 0755
INSTOWNER = root
INSTGROUP = root

all: $(PROGS)

$(PROGS): main.o
$(CC) main.o

main.o: main.c
$(CC) $(CFLAGS) main.c

install: $(PROGS)
$(INSTALL) $(INSTDIR)
$(INSTALL) $(INSTMODE) $(INSTOWNER) -o $(INSTGROUP) $(PROGS) $(INSTDIR)

clean:
rm -rf $(PROGS) *o $(PROGS)

--So, where are the errors

Thank you in advance,
Spiros

tronayne 06-03-2011 06:23 AM

I suspect that
Code:

INSTDIR = $(prefix)/usr/bin/
is giving you a problem (the $(prefix) appears undefined and the trailing / doesn't need to be there).

Here's a Makefile that does it all that may help
Code:

MAKEFILE =      Makefile

# We use ${HOME}, you can use /usr, /usr/local, etc. Note ${BINDIR} below
BASDIR  =      ${HOME}

LDFLAGS =      -lm

OWNER  =      ${LOGNAME}

GROUP  =      ${GRPNAME}

BINDIR  =      $(BASDIR)/bin

BMASK  =      0755

MAINS  =      sample

OBJECTS =      caldat.o julday.o sample.o

SOURCES =      caldat.c julday.c sample.c

ALL:            $(MAINS)

sample:        sample.o caldat.o julday.o $(LIBRARY)
        $(CC) $(CFLAGS) -s -o sample sample.o caldat.o julday.o $(LDFLAGS)


caldat.o:

julday.o:

sample.o:

install:        $(MAINS)
        cp $(MAINS) $(BINDIR)
        cd $(BINDIR);\
        chown $(OWNER) $(MAINS);\
        chgrp $(GROUP) $(MAINS);\
        chmod $(BMASK) $(MAINS)

remove:
        cd $(BINDIR);\
        rm -f $(MAINS)

clean:
        rm -f $(OBJECTS)

clobber:
        rm -f $(OBJECTS) $(MAINS)

The above follows the AT&T Bell Labs method.

Note that this consturct
Code:

$(CC) $(CFLAGS) -s -o
"strips" information from the executable (makes it smaller); a reasonable thing to do if you're not using profiling or debugging -- and don't do it if you are using either of those.

By the way, it's helpful if you enclose code samples in code blocks (easier to read).

Hope this helps some.

spiros 06-03-2011 09:54 AM

Problem solved !
 
I modified the code given by tronayne and it works ! The user can now use make install from within the directory containing the source code and the Makefile and after installation, all the mess can be cleaned up using make remove and make clean or make clobber.

Many thanks to user tronayne !!!

Here is my new Makefile:


MAKEFILE = Makefile


CC=gcc
CFLAGS = -c -Wall
LDFLAGS = -lm

OWNER = root
GROUP = root
BMASK = 0755

MAINS = hello123 #binary name
SOURCEDIR = ${PWD}
TARGETDIR = /usr/bin

OBJECTS = main.o
SOURCES = main.c

ALL: $(MAINS)

hello123: main.o $(LIBRARY)
$(CC) -o hello123 main.o $(LDFLAGS)

main.o:

install: $(MAINS)
cd /
cp $(SOURCEDIR)/$(MAINS) $(TARGETDIR)
cd /usr/bin;\
chown $(OWNER) $(MAINS);\
chgrp $(GROUP) $(MAINS);\
chmod $(BMASK) $(MAINS)
remove:
cd $(SOURCEDIR);\
rm -f $(MAINS)
clean:
rm -f $(OBJECTS)
clobber:
rm -f $(OBJECTS) $(MAINS)

tronayne 06-03-2011 11:37 AM

Glad to know it helped.

One comment: in your Makefile you have
Code:

remove:
cd $(SOURCEDIR);\
rm -f $(MAINS)

remove is meant to remove an installed executable from $(BINDIR) (you occasionally may want to do that); it's analogous to make uninstall.

The clobber directive is meant to remove both the object and executable file(s) create by make -- idea being to keep your source code directory size to a minimum.

Just a thought.


All times are GMT -5. The time now is 11:39 PM.