Dual Makefile

From wiki.gp2x.org

If you want to compile a PC and GP2X Version with one make run, here's a quick makefile on how to do so:

Add each source file in your sources to the line beginning SOURCES. Someone please improve this soon :)

PROG_NAME = helloworld
SOURCES = hello.cpp

OBJECTS = $(SOURCES:.cpp=.o)
BIN = $(PROG_NAME)
BIN_GP = $(PROG_NAME).gpe

CROSS_PATH = /opt/open2x/gcc-4.1.1-glibc-2.3.6/

CXXFLAGS = -c -Wall -Wextra
LDFLAGS = -lm -lg -lpthread -lSDL
LDFLAGS_GP = -DGP2X `$(CROSS_PATH)/bin/sdl-config --cflags` `$(CROSS_PATH)/bin/sdl-config --static-libs`


ifdef arm
CXX = $(CROSS_PATH)/bin/arm-open2x-linux-g++
CXXFLAGS += -I$(CROSS_PATH)/include
else
CXX = g++
CXXFLAGS += `sdl-config --cflags`
endif

all: 
	make pcversion
	make cleanobjs
	make gpversion arm=1

pcversion: $(SOURCES) $(BIN)

gpversion: $(SOURCES) $(BIN_GP)

$(BIN): $(OBJECTS) 
	$(CXX) $(LDFLAGS) $(OBJECTS) -o $@


$(BIN_GP): $(OBJECTS) 
	$(CXX) $(LDFLAGS_GP) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
	$(CXX) $(CXXFLAGS) $< -o $@

cleanobjs:
	rm -f $(OBJECTS)

clean:
	-rm -f $(BIN_GP) $(BIN) *~ *.o *.bak

The Makefile runs twice. After the first compile for the PC, it calls itself passing an argument of 'arm=1', which compiles for the gp2x on the second pass. The argument -DGP2X enables button mapping in the code, wrap the GP2X code with this preprocessor directive, otherwise use gamepad SDL mapping.

#ifdef GP2X
//GP2X Button Mapping
#else
//Standard Gamepad SDL Mapping
#endif
Personal tools