java parse makefile,Java Makefile基础

本文解答了Java初学者关于Makefile的常见疑问,并解释了为何Makefile不适合用于Java项目构建,建议使用Ant或Maven等专业工具。文章还探讨了Makefile的基本概念,如目标文件类型、编译流程及Makefile的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am new to java and makefile, and I'd like some introductory information about a makefile. The information that's typically found online are mostly examples and high level. Here's some specific/naive questions I have about makefile:

What type of files are targets of a makefile? .class and .java?

What does a makefile do to those project files? I did my projects in Eclipse, so the .class files are stored in bin, and .java files are stored in scr. What kind of connection does makefile make between these files?

What is the result of a makefile? makefile's purpose is tell compiler how to make an executable, so the result is a new executable file in the current directory, yes?

Where should I place the makefile? In the bin folder? scr folder? mingle all flies under one directory and put it under there?

lastly, I made a small makefile for my project which should compile 3 classes. It's not working and I don't know why:

makefile:

JFLAGS = -g

JC = javac

.SUFFIXES: .java .class

.java.class:

$(JC) $(JFLAGS) $*.java

CLASSES = \

DNA.java \

C.java \

fastmatch.java

default: classes

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

clean:

$(RM) *.class

Did I do something wrong? I just put everything under one folder and typed "make" in command line. I think i'm missing some big picture here so I'd really appreciate if someone can explain them to me. Thanks

解决方案

make is not the correct tool for building Java. You should use Ant or Maven or another Java-building tool. It is possible to get the job done with make, but for anything but the most trivial Java programs, you will end up with a makefile that deletes everything and recompiles from scratch every time.

Make works well in the land of C and friends. There, a source file depends on all the headers that it imports, and the headers depend on nothing (because they don't get compiled), so your dependency tree is not excessively deep. In Java, every class depends on every class it imports, which depend on further classes, so it's frequently the case that a single class can't be compiled without compiling every other class in the project. You could describe that in your makefile, but it would be hours of tedious work to do what Ant does in less than a second.

All that said:

"targets" are things that the makefile is generating. If you're not writing code that writes code, then the .java files are not the targets

in the makefile you've posted, .class files will end up in the same location as corresponding .java files

the result of a makefile can basically be anything at all. make is a fairly powerful language in its own right. The result of this makefile is three compiled Java classes (possibly more, because javac might find more classes to compile or some of your classes might have inner classes. Again, this is why make is a bad tool for Java)

a makefile typically lives in the root project directory. Often, complex projects will have other makefiles for sub-directories

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值