Makefile

From ITRS
Jump to: navigation, search

Makefile

一般Linux 下 C/C++ 開發用 Makefile

CFLAGS := -Wall -g -Os -Wp,-D_FORTIFY_SOURCE=2
CFLAGS += -fexceptions -fstack-protector --param=ssp-buffer-size=4
CXXFLAGS := $(CFLAGS)
LDFLAGS := -lm
EXECUTABLES := $(basename $(wildcard *.c *.cpp))
all: $(EXECUTABLES)
clean:
rm -f $(EXECUTABLES) core.*
.PHONY: all clean

AVR / Arduino 的 Makefile

SERIAL_CONNECTION_TO_ARDUIONO := /dev/ttyUSB0 #要連到的port
F_CPU := 16000000UL                           #CPU frequency
MCU := atmega168
CROSS_COMPILE := avr-
CC := $(CROSS_COMPILE)gcc
STRIP := $(CROSS_COMPILE)strip
STRIP := $(CROSS_COMPILE)strip
OBJCOPY := $(CROSS_COMPILE)objcopy

CFLAGS := -Wall -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU)

%-stripped: %
	$(STRIP) $^ -o $@

%.srec: %-stripped
	$(OBJCOPY) -O srec $^ $@

%.hex: %-stripped
	$(OBJCOPY) -O ihex $^ $@

PROGRAM := hello                          # program name
all: $(PROGRAM).hex
$(PROGRAM).hex: $(PROGRAM)-stripped
$(PROGRAM)-stripped: $(PROGRAM)
$(PROGRAM): $(PROGRAM).c uart.c           # file that depended on 倚賴的檔案

clean:
	rm -f $(PROGRAM) $(PROGRAM)-stripped $(PROGRAM).hex

install :
#	avrdude -p atmega168 -c stk500v2 -P com4 -U $(PROGRAM).hex
	avrdude -p m168 -b 19200 -P $(SERIAL_CONNECTION_TO_ARDUIONO) -c stk500v1 -F -u -U flash:w:$(PROGRAM).hex

.PHONY: all clean install

VIM 設定

$HOME/.vimrc

map \t :make install<CR>
map \m :w<CR>:make<CR>:cwindow<CR>

Java Makefile

JC = javac
JFLAGS = -Xlint

PROGRAMS = $(shell ls | grep .*.java)
all: $(PROGRAMS: .java=.class)

%.class: %.java
    $(JC) $(JFLAGS) $^

See Also

Comments

blog comments powered by Disqus