Makefile

From WxWiki
Jump to: navigation, search

This is an example of a quite generic GNU Makefile


 CXX = $(shell wx-config --cxx)
 
 PROGRAM = yourproject
 
 OBJECTS = $(PROGRAM).o
 
 # implementation
 
 .SUFFIXES:      .o .cpp
 
 .cpp.o :
         $(CXX) -c `wx-config --cxxflags` -o [email protected] $<
 
 all:    $(PROGRAM)
 
 $(PROGRAM):     $(OBJECTS)
         $(CXX) -o $(PROGRAM) $(OBJECTS) `wx-config --libs`
 
 clean:
         rm -f *.o $(PROGRAM)


To tell the Makefile that every .cpp file must be made into an object, you can define OBJECTS as:

OBJECTS := $(patsubst %.cpp,%.o,$(wildcard *.cpp))

You could have problem to compile with 'wx-config --sth' because GCC try to compile a file called "wx-config --sth". Then you can use it as variable :

CXX = $(shell wx-config --cxx)
PROGRAM = yourproject
# wx-config --libs
WX_LIBS = $(shell wx-config --libs)
# wx-config --cxxflags
WX_CXXFLAGS = $(shell wx-config --cxxflags)

OBJECTS = $(PROGRAM).o

# implementation

.SUFFIXES:      .o .cpp

.cpp.o :
	$(CXX) $(WX_CXXFLAGS) -c  -o [email protected] $<

all:	$(PROGRAM)

$(PROGRAM):$(OBJECTS)
	$(CXX) $(WX_LIBS) -o $(PROGRAM) $(OBJECTS)

Alternative makefile

This makefile uses gcc to include dependecies for the source files in the makefile, and allows you to specify which objects you want to use for every target.

# Define default flags (include your source tree for example
INCFLAGS = -I./src
CXXFLAGS = `wx-config --cxxflags`
LDFLAGS  = `wx-config --ldflags`
LDLIBS   = `wx-config --libs`
REZFLAGS = `wx-config --rez-flags`

ifdef FINAL
    EXTRAFLAGS = -MD -O2 -fno-rtti -fno-exceptions -fomit-frame-pointer
else
    EXTRAFLAGS = -MD -g
endif

# Define our sources, calculate the dependecy files and object files
TEST_SOURCES := src/Sourcefile1.cc src/Sourcefile2.cc src/MainSourceFile.cc 
TEST_OBJECTS := $(patsubst %.cc, %.o, ${TEST_SOURCES})
TEST_DEPS := $(patsubst %.cc, %.d, ${TEST_SOURCES})

#include our project dependecy files
-include $(TEST_DEPS)

all: test

test: $(TEST_OBJECTS)
    $(CXX) $(LDFLAGS) -o test $(TEST_OBJECTS) Test_resources.o $(LDLIBS)
ifdef FINAL
        strip test.exe
endif

Test_resources.o: Test.rc
    windres -i Test.rc -o Test_resources.o $(REZFLAGS)

clean:
    rm -rf test.exe test $(TEST_OBJECTS) $(TEST_DEPS) Test_resources.o

Bakefiles

Moved to Bakefile