any makefile gurus?

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does anyone know how to make a makefile that handles source files that are in several folders?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Create An AVRGCC Project in AVR Studio. Add to the project some files from different folders and then look at the generated makefile.

Regards
Sebastian

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just tried that. Studio's makefile doesn't build and it doesn't look like it could ever build. The makefile seems to assume all the source files are in the same folder as the makefile.

I'm not very familiar with AVR Studio so maybe I'm doing something wrong. However I'll bet AVR Studio is as clueless as I am. I'd like to be wrong though. Can anyone get AVR Studio to use source files in folders different from the "project" folder?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try using the Mfile app from the WinAVR installation. In the generated makefile, there is a line:

# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c

which you could easily change to:

# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c foo/bar.c gort/grunge.c apple/sauce.c

A later line in the makefile generates the object file names from the source file names:

# Define all object files.
OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o) 

(where OBJDIR is defined earlier as . -- you could change this to ./obj or anything else you want)

Try using the makefile from Mfile. You will need to point AVRStudio to the external makefile, but that's relatively easy.

Hope this help!

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for all the suggestions. I've tried about everything including Mfile. I tried that a couple of days ago. I think the problem with Mfile is the use of OBJDIR. The way it's used produces strange results when the source is not in the "project" folder.

However using AVR Studio to make the makefile is starting to show some promise. I added VPATH and that got it to build.

There is an extra complication with Studio's makefile but that's my problem and hopefully I can fix it. Studio's makefile is for C. All my code is C++. However I came up with some quick and dirty CPPFLAGS and it now builds. Also the "clean" works. The only thing that doesn't work so far is the dependency thing. I can live without that by doing a clean before running make.

I guess the dependency stuff is done by this line:
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d

I simply changed CFLAGS to CPPFLAGS and added it to my CPPFLAGS. Maybe those options need to be changed for C++.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Generally the reason a build fails like this is because gcc cannot create a path to the object file it creates. Thus, if your objdir is anything other than ./ it will fail.

If your source is at foo/bar.c then the object for that file is converted to foo/bar.o in the makefile

When gcc tries to create the object file, it tries to create objdir/foo/bar.o

GCC cannot create directories in the path to the object file, so foo must already exist as a subdir of objdir.

The easiest way is to have objdir empty, or set to the base of your source tree.

To create all objects in objdir, all your source files must have unique names, and you must do some string manipulation on the names to strip out the path information to create a raw object name.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's what happens when OBJDIR = .

It builds okay. It also does the dependency stuff okay. I guess I could live with that.

But it puts the .o and .lst files in the folder where the source is located, and clean doesn't remove them. That's a problem because I will be building for various targets and I need to clean the .o files.
I suppose I could fix that by modifying the clean commands in the makefile.

Actually I like the AVR Studio way better in one respect. AVR Studio's makefile, after I specify the source folders with VPATH, puts the .o and .lst files into the project folder. That's preferred because I'll only have the makefile and the compiler output there. Also clean works. I will be using a different "project" for each target so I don't need to do a clean anyway. The problem I have so far with this method is the dependency stuff doesn't work, but maybe I can get it going.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay. I think I have it. Thanks to those who posted. I had been playing around with this for two days and going insane.

It seems the AVR Studio makefile does do the dependencies. It works perfectly and does just what I want. I did however need to specify the source folders with VPATH.

The OBJDIR type makefile also works when I set:
OBJDIR = .
except the clean needs fixing.

However this makefile puts the .o files in the source folders whereas the Studio makefile puts the .o files in the project folder. The .o files really belong with the project, not the source, because I use the source for many projects.

Again thank you, thank you. I'm slowly getting my sanity back.