Friday, April 25, 2008

Sample Makefile

Some Information about Makefile's =================================
  1. Type "make" on the command line to make the project.
  2. You need to install the GNU make utility.
  3. Type "make clean" to remove object files.
  4. @ suppresses output.
  5. pkg-config automatically finds the required compiler and linker flags for developing against a library.
  6. Issue "export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/" if pkg-config can't find the libraries This directory contains *.pc files.
  7. $< : an element of the dependencies (to the right of : ) $^ : all dependencies $@ : target (to the left of : )
  8. Command prototype is : targets : prerequisites TAB command
  9. Tabs must be used for indentation; not spaces.
  10. CPPFLAGS are preprocessor flags, CFLAGS are flags for the C compiler (also to be supplied at linking time), CXXFLAGS are flags for the C++ compiler (also to be supplied at linking time)
  11. If you have a single C++ file, compile using (no need for a Makefile): g++ `pkg-config --cflags itpp` -o my_prog my_prog.cpp `pkg-config --libs itpp`
Tabs are important for Makefiles; copying-pasting the following won't work since tabs are gone in blogger. (or download it here)

CXX=@g++
CPPFLAGS=$(shell pkg-config --cflags itpp)
CXXFLAGS=-Wall
LDFLAGS=$(shell pkg-config --libs itpp)

SOURCES=  $(wildcard *.cpp) # Or manually list the source files here
OBJECT=$(SOURCES:.cpp=.o)
OBJECT_DIR=obj
OBJECTS= $(patsubst %,$(OBJECT_DIR)/%,$(OBJECT))
EXECUTABLE=../btctest

all: $(SOURCES) $(EXECUTABLE)
INSERT TAB HERE@echo Finished without errors.

$(EXECUTABLE) : $(OBJECTS)
INSERT TAB HERE @echo Linking...
INSERT TAB HERE$(CXX) $(CXXFLAGS) $(LDFLAGS) $^ -o $@

$(OBJECT_DIR)/%.o: %.cpp
INSERT TAB HERE @echo Compiling $^
INSERT TAB HERE $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@   dist : $(SOURCES) $(EXECUTABLE) $(OBJECTS)  INSERT TAB HEREcd ../.. && tar -cjf a.tar.bz2 btc_itpp/src/ btc_itpp/btctest btc_itpp/Makefile
.PHONY: clean
clean:
INSERT TAB HERE @echo Cleaning object files...
INSERT TAB HERE @rm -rf $(OBJECTS)


To call it from parent dir:

# This file calls the other Makefile inside the src/ 

all:
INSERT TAB HERE@$(MAKE) -C src

clean:
INSERT TAB HERE @$(MAKE) -C src clean

dist:
INSERT TAB HERE @$(MAKE) -C src dist

doc: src
INSERT TAB HERE /Applications/Doxygen.app/Contents/Resources/doxygen Doxyfile

No comments: