Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dbrayford

macrumors member
Original poster
Feb 22, 2010
41
0
Hi,

I am new to writing makefiles and am struggling to write a working makefile that compiles and links several cpp, mm and m files to create a dylib binary.
The makefile I've written is below

Cheers
David


Code:
 all: $(SOURCESCPP) $(OBJECTSCPP)
 
CC=g++

CCFLAGS=-c -Wall -m64 

INCPATH=-I/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Headers/ -I/Developer/SDKs/MacOSX10.5.sdk/usr/X11/include

LDFLAGS=-dynamiclib -framework AppKit -framework OpenGL -framework Foundation -framework JavaVM

SOURCESCPP=Images.cpp

SOURCESMM=GraphicsContextProperties.mm GraphicsContextPropertiesList.mm MyGL.mm

SOURCESM=OpenGLAlertsUtilityToolkit.m OpenGLController.m


OBJECTSCPP=$(SOURCESCPP:.cpp=.o) 
OBJECTSMM=$(SOURCESMM:.mm=.o) 
OBJECTSM=$(SOURCESM:.m=.o)

.cpp.o:
	$(CC) $(CCFLAGS) $(INCPATH) $<

.mm.o:
	$(CC) $(CCFLAGS) $(INCPATH) $<

.m.o:
	$(CC) $(CCFLAGS) $(INCPATH) $<


LIBRARY=-o libAvistoGL.dylib

 clean: 
	rm -rf *.o *.dylib
 
Sorry about that. The message on the commandline is

make: Nothing to be done for 'all'.
 
You're close. That error is coming up because the 'all' dependencies haven't been defined yet (i.e. make doesn't know what SOURCESCPP or OBJECTSCPP are yet). Not only that, you haven't defined a library target. So, make wouldn't know how to actually create the library even if it did compile the source files.

Few things:

all doesn't need to depend on SOURCESCPP or OBJECTSCPP. It should depend on LIBRARY. If you were building an application, then all would depend on the executable. LIBRARY should be redefined to just be the name of the lib (i.e. take out the -o). Create a LIBRARY target and have it depend on OBJECTSCPP. The LIBRARY target should contain the command to build the library. Move the all target so it is after all of its dependency definitions.

Something like this:

Code:
LIBRARY=libAvistoGL.dylib

all: $(LIBRARY)

$(LIBRARY): $(OBJECTSCPP)
           $(CC) $(LDFLAGS) $(OBJECTSCPP) -o $@

That's all I got from quickly looking at it. I think it will work if you make those changes. I didn't take into account your .m and .mm files. You'll need to add those object files to the LIBRARY target as well.

PS. This is purely technique, but I'd move the SOURCES definitions to the very top of the file so it is the first thing that other users will see. Reason being, because SOURCES is typically the variable that most users will need to edit over time.
 
I've modified the makefile, but still get the same message.
Code:
 all: $(LIBRARY)
 
CC=g++

CCFLAGS=-c -Wall -m64 

INCPATH=-I/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Headers/ -I/Developer/SDKs/MacOSX10.5.sdk/usr/X11/include

LDFLAGS=-dynamiclib -framework AppKit -framework OpenGL -framework Foundation -framework JavaVM

SOURCESCPP=Images.cpp

SOURCESMM=GraphicsContextProperties.mm GraphicsContextPropertiesList.mm AvistoGL.mm

SOURCESM=OpenGLAlertsUtilityToolkit.m OpenGLController.m


OBJECTSCPP=$(SOURCESCPP:.cpp=.o) 
OBJECTSMM=$(SOURCESMM:.mm=.o) 
OBJECTSM=$(SOURCESM:.m=.o)

.cpp.o:
	$(CC) $(CCFLAGS) $(INCPATH) $<

.mm.o:
	$(CC) $(CCFLAGS) $(INCPATH) $<

.m.o:
	$(CC) $(CCFLAGS) $(INCPATH) $<


LIBRARY=libAvistoGL.dylib

$(LIBRARY): $(OBJECTSCPP) $(OBJECTSM) $(OBJECTSMM)
	$(CC) $(LDFLAGS) $(OBJECTSCPP) $OBJECTSM) $(OBJECTSMM) -o $@

 clean: 
	rm -rf *.o *.dylib
 
Move the all target down below the LIBRARY definition. Sorry. My first reply didn't mention that and I probably edited it after you read it. Everything else looks good.

I'd actually move the LIBRARY and SOURCES definitions so they are at the very top of the file, but that's a personal choice. That way, if you ever want to re-use the makefile for something else, everything that you would need to change is at the very top. It's also easier for future readers to see what the makefile is actually building that way.
 
You might also have to remove the space before 'all:' and 'clean:'.

IIRC, make is somewhat sensitive to leading white-space.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.