0

How do I add permissions to this simple make file?

all: shell ls cat groups

shell: shell.o cd.o
    gcc shell.o cd.o -o shell

cat: cat.o 
    gcc -c cat.c -o cat

ls: ls.o header.h
    gcc -c ls.c -o ls

groups: groups.o groups.h 
    gcc -c groups.c -o groups

shell.o: shell.c tlpi_hdr.h
    gcc -c shell.c

cd.o: cd.c tlpi_hdr.h
    gcc -c cd.c

cat.o: cat.c tlpi_hdr.h
    gcc -c cat.c 

ls.o: ls.c header.h
    gcc -c ls.c

groups.o: groups.c groups.h
    gcc -c groups.c 

clean: 
    rm *.o shell cat ls groups

I need to run cat/ls/groups/cd within the shell that I created, but can't because it doesn't have permissions. How can I do this?

Wouter Verhelst
  • 1,269
  • 1
  • 12
  • 27
  • 2
    The compiler normally makes the programs it produces executable so you don't need to. You may need to add the directory to your `PATH` though. How is your `cd` going to work as a separate binary for? – Jonathan Leffler Dec 10 '16 at 05:15
  • Learn more about `make` and its builtin rules. Try `make -p`. See [this](http://stackoverflow.com/a/16751650/841108) – Basile Starynkevitch Dec 10 '16 at 08:19
  • Read [Advanced Linux Programming](http://advancedlinuxprogramming.com/). Notice that `cd` cannot be an external program (it has to be a shell builtin). – Basile Starynkevitch Dec 10 '16 at 08:22

1 Answers1

1

As an example, this rule:

groups: groups.o groups.h 
    gcc -c groups.c -o groups

tells GCC to produce an object file called groups (instead of the normal groups.o) because you included the -c option. Object files are not executables, so the compiler doesn't make them executable, hence your problem with the permissions. Note that the rule requires make to build groups.o, but then proceeds to ignore the object file because it recompiles the source code.

You wanted to write something more like:

groups: groups.o 
    gcc groups.o -o groups
groups.o: groups.h

You need to use a lot more macros — or you could leave the rules out of the makefile since make knows how to build groups from groups.c.

You should also be compiling with warning options. At minimum -Wall; preferably -Wall -Wextra -Werror (and I use still more options than these). You might want optimization (-O3); you might want debugging (-g). These are normally handled via setting macros and then using them in the commands.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278