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