View previous topic :: View next topic |
Author |
Message |
^marcs Apprentice
Joined: 09 Mar 2005 Posts: 169
|
Posted: Fri Nov 04, 2011 6:28 pm Post subject: Makefile and .cpp<->.o locations |
|
|
Hello,
I'm having a little trouble mastering where my compiled .o files lands within a source tree.
Let's assume I have this almost empty source tree:
Code: | /Release/ <<empty, object files should land here
/myClasses/class.cpp
/main.cpp
|
Now, when I invoke 'make' all cpp file get compiled and their respective objects files goes to ./Release directory, BUT sub directories in source tree are ignored in their "object" mirror.
What I mean:
Compiled tree looks like this
Code: | /Release/class.o
/Release/main.o
/myClasses/class.cpp
/main.cpp
|
when it should look like this
Code: | /Release/myClasses/class.o << additional subdir "myClasses" like in src tree.
/Release/main.o
/myClasses/class.cpp
/main.cpp
|
Why this is a problem? Bcos when I modify main.cpp and invoke 'make' again, it looks for compiled objects (for all files) in ./Release directory, but with their respective sub directories and ofc it does not find it, so class.cpp gets compiled again (and all other src files that are elsewhere than root dir).
If anyone could help with it, here are paste links :
actual Makefile: http://pastebin.com/j1Mj12d8
Makerules (its included in Makefile in last line) http://pastebin.com/jWAmZSiM
FYI, it's more or less a template makefile for avr projects, yet it should not make any difference considering this problem. |
|
Back to top |
|
|
wthrowe Tux's lil' helper
Joined: 19 Aug 2009 Posts: 141
|
Posted: Sat Nov 05, 2011 9:23 pm Post subject: |
|
|
I think I see the problem (haven't actually tested, though). Compare these two sections from your Makerules (starting line 483): Code: | # Compile: create object files from C source files.
$(OBJDIR)/%.o : %.c
@echo
@echo $(MSG_COMPILING) $<
$(CC) -c $(ALL_CFLAGS) $< -o $@
# Compile: create object files from C++ source files.
$(OBJDIR)/%.o : %.cpp
@echo
@echo $(MSG_COMPILING_CPP) $<
$(CC) -c $(ALL_CPPFLAGS) $< -o $(OBJDIR)/$(@F) |
Foe .c files, the object file is written to $@, which is the target of the rule. This is what you want. For .cpp files, however, the object file is written to $(OBJDIR)/$(@F). $(@F) is the basename (including suffix) of the target, so all the object files from .cpp sources go to the same directory. Try changing the last line of the above to Code: | $(CC) -c $(ALL_CPPFLAGS) $< -o $@ | (With a leading tab instead of spaces, of course) and see if that helps.
If you get errors about the directories not existing you may also need another line something like @mkdir -p $(@D) just before the $(CC) line. |
|
Back to top |
|
|
^marcs Apprentice
Joined: 09 Mar 2005 Posts: 169
|
Posted: Sun Nov 06, 2011 8:07 am Post subject: |
|
|
Thanks for reply!
Your soultion almost working, .o files lands where they should, but we need to modify linker command, atm. it looks for object files only in ./Release
i think modification required is somewhere around line 474
Code: | # Link: create ELF output file from object files.
.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)
%.elf: $(OBJ)
@echo
@echo $(MSG_LINKING) $@
$(CC) $(ALL_CFLAGS) $(patsubst %, $(OBJDIR)/%, $(notdir $^)) $(EXTRAOBJFILES) --output $@ $(LDFLAGS) |
|
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21595
|
Posted: Sun Nov 06, 2011 4:43 pm Post subject: |
|
|
Code: | $(CC) $(ALL_CFLAGS) $(LDFLAGS) $^ -o $@ | I intentionally dropped EXTRAOBJFILES because relying on files not in your dependency list can break parallel builds. If your target needs the files listed in that variable, put them in the dependency list so that $^ will include them. Also, I relocated LDFLAGS closer to the front, so that any position-specific flags are seen before the files are listed. |
|
Back to top |
|
|
^marcs Apprentice
Joined: 09 Mar 2005 Posts: 169
|
Posted: Sun Nov 06, 2011 9:07 pm Post subject: |
|
|
\o/
It's working well, major part working, thanks! You guys are so awesome
Athough everyting I need is working, one minor inconvenience left - 'make claen' tends to leave .o files that are elsewhere than ./Release/
make clean entry for .o files is in Makerules:534 if You might take a look.
atm. i just added Code: | $(REMOVEDIR) $(OBJDIR)/* | at the end, but this sounds pretty dirty. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|