注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

liangxh2008的博客

 
 
 

日志

 
 

Creating a Makefile for a set of Java files  

2010-05-18 14:31:47|  分类: java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |


Here is an example makefile for a Java program that usesa few class files:


JFLAGS = -g
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java

CLASSES = \
Purchaser.java \
PurchaserList.java \
PurchaserListTest.java \
SortedList.java \
Supplier.java \
SupplierList.java \
LinearList.java

default: classes

classes: $(CLASSES:.java=.class)

clean:
$(RM) *.class

To use the above makefile to compile your java programs:
  1. create a file named 'makefile' in your lab directory with the same content as this example
  2. modify the CLASSES macro so that it has the names of your .java files;
  3. run 'make', and if all goes well, it should compile all of your java source files that need to be re-built.

Here is the same makefile, with comments explaining each line:

# define a variable for compiler flags (JFLAGS)
# define a variable for the compiler (JC)
#
JFLAGS = -g
JC = javac
#
# Clear any default targets for building .class files from .java files; we
# will provide our own target entry to do this in this makefile.
# make has a set of default targets for different suffixes (like .c.o)
# Currently, clearing the default for .java.class is not necessary since
# make does not have a definition for this target, but later versions of
# make may, so it doesn't hurt to make sure that we clear any default
# definitions for these
#
.SUFFIXES: .java .class
#
# Here is our target entry for creating .class files from .java files
# This is a target entry that uses the suffix rule syntax:
# DSTS:
# rule
# DSTS (Dependency Suffix Target Suffix)
# 'TS' is the suffix of the target file, 'DS' is the suffix of the dependency
# file, and 'rule' is the rule for building a target
# '$*' is a built-in macro that gets the basename of the current target
# Remember that there must be a < tab > before the command line ('rule')
#
.java.class:
$(JC) $(JFLAGS) $*.java
#
# CLASSES is a macro consisting of several words (one for each java source file)
# the backslash "\" at the end of the line is a line continuation character
* so that the same line can continue over several lines
CLASSES = \
Purchaser.java \
PurchaserList.java \
PurchaserListTest.java \
SortedList.java \
Supplier.java \
SupplierList.java \
LinearList.java
#
# the default make target entry
# for this example it is the target classes
default: classes
# Next line is a target dependency line
# This target entry uses Suffix Replacement within a macro:
# $(macroname:string1=string2)
# In the words in the macro named 'macroname' replace 'string1' with 'string2'
# Below we are replacing the suffix .java of all words in the macro CLASSES
# with the .class suffix
#
classes: $(CLASSES:.java=.class)
# this line is to remove all unneeded files from
# the directory when we are finished executing(saves space)
# and "cleans up" the directory of unneeded .class files
# RM is a predefined macro in make (RM = rm -f)
#
clean:
$(RM) *.class
  评论这张
 
阅读(446)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017