More on Makefiles

Posted by Robert Powell on Mon 28 Nov 2005 09:42 AM — 3 posts, 13,368 views.

Australia #0
Seeing that we have been talking makefiles, i would like to move some of my .c files into there own directories so that there is less clutter in the /src directory.

Now i really know very little about makefiles and don't have the faintest idea on how to make this work. If anyone could give me a few pointers on how to do this based on the standard smaugfuss makefile would be most apprecated.

Thanks in advance.
Australia Forum Administrator #1
I'm not the world's biggest expert on Makefiles, but this seems to work. Don't copy and paste from below as you need tabs at the start of most lines, not spaces. However the changes I think I made are in bold, just copy them into your makefile:


CC      = gcc
#PROF    = -p

#Uncomment to compile in Cygwin
#CYGWIN = -DCYGWIN

# Uncomment the two lines below if compiling on a Solaris box
#SOLARIS_FLAG = -Dsun -DSYSV
#SOLARIS_LINK = -lnsl -lsocket

#IMC2 - Comment out to disable IMC2 support
IMC = 1

W_FLAGS = -Wall -Werror -Wshadow -Wformat-security -Wpointer-arith -Wcast-align -Wredundant-decls -Wbad-function-cast

C_FLAGS = -g3 $(W_FLAGS) $(SOLARIS_FLAG) $(PROF)
L_FLAGS = $(PROF) $(SOLARIS_LINK) -lz

C_FILES = act_comm.c act_info.c act_move.c act_obj.c act_wiz.c ban.c boards.c \
          build.c clans.c color.c comm.c comments.c const.c db.c deity.c fight.c \
          handler.c hashstr.c hotboot.c imm_host.c interp.c magic.c makeobjs.c mapout.c mccp.c \
          md5.c misc.c mpxset.c mud_comm.c mud_prog.c planes.c player.c polymorph.c \
          reset.c save.c services.c shops.c skills.c special.c tables.c \
          track.c update.c

ifdef IMC
   C_FILES := imc.c $(C_FILES)
   C_FLAGS := $(C_FLAGS) -DIMC -DIMCSMAUG
endif

O_FILES := $(patsubst %.c,o/%.o,$(C_FILES))

SRC_FILES := $(patsubst %.c,src/%.c,$(C_FILES))

H_FILES = $(wildcard src/*.h)

all:
        $(MAKE) -s smaug

# pull in dependency info for *existing* .o files
-include dependencies.d

ifdef CYGWIN
smaug: $(O_FILES)
        rm -f smaug.exe
        $(CC) -o smaug.exe $(O_FILES) $(L_FLAGS)
        echo "Making dependencies ...";
        $(CC) -MM $(CFLAGS) src/*.c > dependencies.d
        perl -pi -e 's.^([a-z]).o/$$1.g' dependencies.d
        echo "Done compiling mud.";
        chmod g+w smaug.exe
        chmod a+x smaug.exe
        chmod g+w $(O_FILES)

clean:
        rm -f o/*.o smaug.exe *~ src/~*
else
smaug: $(O_FILES)
        rm -f smaug
        $(CC) -o smaug $(O_FILES) $(L_FLAGS)
        echo "Making dependencies ...";
        $(CC) -MM $(CFLAGS) src/*.c > dependencies.d
        perl -pi -e 's.^([a-z]).o/$$1.g' dependencies.d
        echo "Done compiling mud.";
        chmod g+w smaug
        chmod a+x smaug
        chmod g+w $(O_FILES)

clean:
        rm -f o/*.o smaug *~ src/*~
        echo "Making dependencies ...";
        $(CC) -MM $(CFLAGS) src/*.c > dependencies.d
        perl -pi -e 's.^([a-z]).o/$$1.g' dependencies.d
endif

indent:
        indent -ts3 -nut -nsaf -nsai -nsaw -npcs -npsl -ncs -nbc -bls -prs -bap -cbi0 -cli3 -bli0 -l125 -lp -i3 -cdb -c1 -cd1 -sc -pmt $(SRC_FILES)
        indent -ts3 -nut -nsaf -nsai -nsaw -npcs -npsl -ncs -nbc -bls -prs -bap -cbi0 -cli3 -bli0 -l125 -lp -i3 -cdb -c1 -cd1 -sc -pmt $(H_FILES)

indentclean:
        rm src/*.c~ src/*.h~

o/%.o: src/%.c
        echo "  Compiling $@....";
        $(CC) -c $(C_FLAGS) $< -o $@



Basically I put "src/" in front of %.c in the line that associates .o files with .c files.

I also had to change the way it generated the dependency file (which isn't in standard SMAUG makefile anyway).

With these changes, you should be able to make a "src" subdirectory (inside src) and move all the .c and .h files into it.
Amended on Mon 28 Nov 2005 08:33 PM by Nick Gammon
Australia #2
Not exactly what i had in mind Nick, I know that i might not have been totally clear on my intentions.

Using your makefile as an example i would like to move say UPDATE.C into a directory off the src directory called update, that compiles the update.c file to update.o which is stored in the o directory off of src.

I know i could do this a number of different ways, multiple makefiles that you call from the main makefile is one way, also below is another way that i worked out i could acheive the desired result, but its way slow because its a full recompile each time.

# make file by Robert Powell
CXX = gcc


# libraries
LIBS = -lm -lcrypt -ldl

# flags
W_FLAGS = -Wall -Wshadow -Wformat-security -Winline -Wpointer-arith -Wcast-align -Wredundant-decls 
CFLAGS = -g2 $(W_FLAGS) $(PROF) -export-dynamic


eldhamud: $(wildcard *.c)
	$(CXX) $(CFLAGS) *.c -c;
	$(CXX) $(CFLAGS) Oasis_Olc/*.c -c;
	mv *.o Object_Files
	$(CXX) -export-dynamic -o eldhamud ./Object_Files/*.o $(LIBS)


clean:
	rm -f *.o eldhamud *~ 
	rm -f Object_Files/*.o 
	rm -f Oasis_Olc/*~
	


I know there has to be an easier method of doing this, and one thats not so hackish as this one above, as sometimes i like to leave stray .c files laying about that i dont want to have compiled with the wildcard method.

Thanks again