09 Mar, 2011, RoFAdmin wrote in the 1st comment:
Votes: 0
So my Hey Everyone-

So my source file directory for my base has become a bit more muddled then i like.
So i decided i want to separate the files up some into sub directories.

I however am unsure of how to get a Makefile to accompany this.
Ive done some research on the net, but i have to admit I'm not very familiar with Makefiles to begin with.
From what ive read the most common way to go about this is to have makefiles in each directory and have
the main makefile include them.

What i was wondering is.

1) Is there a existing base that also has its source split into multiple directories so i can look at it an see how they go about it?
2) Is there a way to do this from within one Makefile instead of one in each directory?
3) And does anyone have a really good resource for makefile for someone who is rather oblivious to them. Seems most tutorials and such i find assume some sort of basic understanding of things that im lacking.

Thanks.
09 Mar, 2011, Runter wrote in the 2nd comment:
Votes: 0
I can't answer most of this, but in my experiences I've found it to be common to use multiple makefiles.
09 Mar, 2011, oenone wrote in the 3rd comment:
Votes: 0
RoFAdmin said:
2) Is there a way to do this from within one Makefile instead of one in each directory?


Should be easy… just use the correct path to the sources.
09 Mar, 2011, Igabod wrote in the 4th comment:
Votes: 0
you should look at my makefile that I uploaded here. It probably won't be what you're looking for in whole, but you can look at it and see how I did the directory split-up for .c and .h and .o files. I commented it a good bit too so it should teach you a little bit too.

http://www.mudbytes.net/file-2597

[edit to add] Oh yeah, and SmaugFUSS has it's directories set up the way you mentioned so if my makefile is a bit too complicated for you to figure out you can always look at that one.
09 Mar, 2011, RoFAdmin wrote in the 5th comment:
Votes: 0
@Igabod:
Thanks for the information.
I found the makefile you linked to to be very interesting to look over, very nice setup indeed.
Did lead me to a couple questions ill ask at the end here though.

Also i looked at smaugFuss, and it didn't seem to have multiple directories set up for the source files, it just seemed to toss the .o files into their own folder. This might be a misunderstanding caused on my part, ill explain what i was trying to do later as well.

@oenone:
Thanks for the input. Though i had already gathered as much. Perhaps though the response is due to my lack of information on exactly what im looking to do, so ill clarify here shortly.

@runter:
Thank you for the input sir, i was suspecting multiple makefiles might be the way i need to go, which leads me to a couple more questions as well.


Ok so what i want to do.
I want to split my source code up into the following directory structure.

src/makefile
src/core/
src/mod_whatever/
src/mod_whatever2/

The core, and mod_whatever directories would contain my split up source code files. Core would contain stuff to the core of the game, and the mod_whatever directories contain modules expanding on the core.

I do have more questions to come, but i want to ponder and research a bit more before i i say something stupid, cause i feel like the answer im seeking is at the tip of my to tongue but i cant spit it out yet. So check back shortly please, lol.

@Igabod:
The more i look at the makefile you provided the more insight it is offering up to me. Thanks again.
09 Mar, 2011, RoFAdmin wrote in the 6th comment:
Votes: 0
Ok so ive pretty much.. almsot got it down. I can make it work actually but im not understanding the how or the why and it seems to be a fluke

Below is my makefile


CC      = g++
PROF = -O -g
C_FLAGS = -Wall -Wno-char-subscripts $(PROF)
L_FLAGS = $(PROF)

EXE_NAME = rof

CORE_OBJ = core/accounts.o core/cmd.o core/cmd_comm.o core/cmd_olc.o core/cmd_olcarea.o \
core/cmd_olcnpc.o core/cmd_olcobj.o core/cmd_olcroom.o core/cmd_staff.o \
core/comm.o core/interp.o core/login.o core/lookup.o core/magic.o \
core/skills.o core/spells.o core/tables.o core/utils.o

CLASSES_OBJ = classes/AREA_DATA.o classes/CHAR_DATA.o classes/MEMORY_HANDLER.o \
classes/MOB_INDEX_DATA.o classes/OBJ_DATA.o classes/OBJ_INDEX_DATA.o \
classes/ROOM_INDEX_DATA.o classes/WORLD_DATA.o \

O_FILES = $(CORE_OBJ) $(CLASSES_OBJ)

$(EXE_NAME): $(O_FILES)
rm -f $(EXE_NAME)
$(CC) $(L_FLAGS) -o $(EXE_NAME) $(O_FILES)


.c.o: core.h
$(CC) -c $(C_FLAGS) $<


Now this works great.. almost, goes along, it makes a bunch of .o files then when it goes to make the exe it freaks out.
Reason being is it decided to make all the .o files in the same directory as the makefile, and linking is looking for them in the place where the
C files are.

Now i can get it to put the .o files in the same palce as the C file by changing

.c.o: core.h

to
obj/.c.o: core.h


Which all together doesn't make sense to me because i was attempting to get it to put them in a directory called obj not the same directory as the c file.


So can anyone direct me in the right direction here. Ideally i would like to have all my .o files go in to and OBJ directory, and have it compile from there.
What am i missing?

Thanks Again.
10 Mar, 2011, Tyche wrote in the 7th comment:
Votes: 0
It'd be helpful if you outline the whole directory structure. Like where is obj? And where do you want the exe?

For example, Is it like this?
.makefile
|
+—src
| +—classes
| +—core
+—obj
| +—classes
| +—core
+—bin
10 Mar, 2011, RoFAdmin wrote in the 8th comment:
Votes: 0
Sure thing.
+—source
+—classes
+—core
+—obj
+—.rof
+—.makefile


There yar go

Thanks again
13 Mar, 2011, RoFAdmin wrote in the 9th comment:
Votes: 0
Hey folks-

Just a little bump here cause this was something i would really like to figure out/understand. Like i said its not huge, i can get it to work, through a reason i dont understand, but i felt it wasnt working as it was supposed to.

In case my directory structure diagram wasnt very clear here is the directory setup.

I have my SOURCE directory. Inside of the SOURCE directory i have three sub-directories (CLASSES, CORE, OBJ) and my makefile also resides in the source directory. All of my source code is in either the CLASSES or CORE directory.

What i would like to do, if possible is have it compile all the .o files into the OBJ directory. Either directly into the OBJ directory if if need be into respective OBJ/CLASSES and OBJ/CORE folders. From there i want it to build the main executable in the SOURCE directory.

What it does currently, is it will either build all the .o files into the SOURCE directory then complain about being unable to link them together cause its looking in the SOURCE/CLASSES and SOURCE/CORE directories for the .o files, or if i make the one tiny adjustment i mentioned before, it will put the .o files in the SOURCE/CLASSES and SOURCE/CORE directories, though that didnt make sense since what i did was try to direct them into the OBJ directory.

Please feel free to point out anywhere im being stupid, and the correct way to go about things. I am far from familiar with make and makefiles, and am unable to grasp what i need from the reading ive done.

Thanks in advance.
13 Mar, 2011, Elervan wrote in the 10th comment:
Votes: 0
Here is the makefile I use that compiles an additional folder inside of the Source folder. It also makes a new folder inside the OBJ folder (where all the .o files go) to house the new folder.

GCC	= gcc
RM = rm
EXE = ../bin/rom

C_FLAGS = -DSYSV -ggdb -w -DNOCRYPT
L_FLAGS = -ggdb -lcrypt -lm -w -DNOCRYPT

#Source Files.
SRC := $(wildcard *.c) $(wildcard clans/*.c)

#Object files.
OBJ_DIR = obj
OBJ := $(patsubst %.c,$(OBJ_DIR)/%.o,$(SRC))

#Make rom
rom: $(OBJ)
$(GCC) $(L_FLAGS) -lcrypt -o $(EXE) $(OBJ)

$(OBJ_DIR)/%.o: %.c
$(GCC) $(C_FLAGS) -c -o $@ $<

clean:
$(RM) -f $(OBJ) $(EXE) *~ *.bak *.orig *.rej


I can't remember if I needed to create the extra folder (clans) inside of obj or not. If so, just create the setup obj obj/classes obj/core, with the above and it should put all the object files where they need to be to link. If you don't like using wildcard I'm sure you can adjust it to list the files your trying to compile.
14 Mar, 2011, RoFAdmin wrote in the 11th comment:
Votes: 0
Hey Everyone-

Just wanted to say thanks for the help, especially Igabod and Elervan.

I can only assume that i had a simple typo error, or something equally mundane causing the problem. After looking at Elervans example, and Igabond makefile. I was like ok i know i already tried this wtf. So i just deleted my makefile and started it over from scratch, and BLAMO worked first time.

Thanks again guys.
14 Mar, 2011, JohnnyStarr wrote in the 12th comment:
Votes: 0
Side note:

There are several IDEs that provide "filters" that serve as folders for separating source files. The actual source
files and headers are still contained in the src folder, so you don't have to do anything special to your linker settings.

MS C++ 2010 Express comes to mind.
14 Mar, 2011, sankoachaea wrote in the 13th comment:
Votes: 0
I was working on getting LuaSocket to compile with MinGW last night and found myself dealing with the same issue. The LuaSocket library is split into multiple makefiles in multiple directories and that was throwing me off for a minute, because I was certain I'd rewritten the root makefile correctly.

Care to post your finished makefile, I'm curious what your typo was.
14 Mar, 2011, RoFAdmin wrote in the 14th comment:
Votes: 0
Not sure what the typo or problem was, but a complete rewrite did fix it.
Here it is though.

CC      = g++
PROF = -O -ggdb
C_FLAGS = -Wall -Wno-char-subscripts $(PROF)

EXE_NAME = rof
OBJ_DIR = obj
SRC := $(wildcard core/*.c) $(wildcard classes/*.c) $(wildcard mod_magic/*.c) $(wildcard mod_olc/*.c)
H_FILES := $(wildcard core/*.h) $(wildcard classes/*.h) $(wildcard mod_magic/*.h) $(wildcard mod_olc/*.h)
OBJ := $(patsubst %.c,$(OBJ_DIR)/%.o,$(SRC))

# COLOR CODES
L_NRM = \e
CC = g++
PROF = -O -ggdb
C_FLAGS = -Wall -Wno-char-subscripts $(PROF)

EXE_NAME = rof
OBJ_DIR = obj
SRC := $(wildcard core/*.c) $(wildcard classes/*.c) $(wildcard mod_magic/*.c) $(wildcard mod_olc/*.c)
H_FILES := $(wildcard core/*.h) $(wildcard classes/*.h) $(wildcard mod_magic/*.h) $(wildcard mod_olc/*.h)
OBJ := $(patsubst %.c,$(OBJ_DIR)/%.o,$(SRC))

# COLOR CODES
L_NRM = \e[0;00m
L_YELLOW = \e[1;33m
L_WHITE = \e[1;37m
L_CYAN = \e[1;36m

$(EXE_NAME): $(OBJ)
@rm -f $(EXE_NAME)
@$(CC) $(PROF) -o $(EXE_NAME) $(OBJ)
@echo -e "$(L_YELLOW)FILE $(EXE_NAME).exe COMPLETE $(L_NRM)"

$(OBJ_DIR)/%.o: %.cpp $(H_FILES)
@echo -e "$(L_WHITE)FILE: $(L_YELLOW)$<$(L_CYAN)"
@$(CC) $(C_FLAGS) -c -o $@ $<

clean:
@rm -f $(EXE_NAME) $(OBJ)
[/code]


Yup thats it. Finally figured it all out, i know what everything is doing and why now. I feel so much smarter. LOL.
15 Mar, 2011, Kline wrote in the 15th comment:
Votes: 0
So I'm late to the party of offering advice here (haven't checked in for a few days, work…blah, lol) but here's a link to my Makefile. I keep header files in their own directory and drop the obj files in a separate one too.

Also, the GNU Make manual has a lot of useful stuff, if not a little archaic to decipher at times. It proved invaluable for me to figure out how to do include/exclude/wildcards and some other optimizations; including the odd format Makefiles use for variables and stuff.
0.0/15