Basics on java makefile

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 05:14:00

问题


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:

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

  2. 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?

  3. 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?

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

  5. 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


回答1:


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:

  1. "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
  2. in the makefile you've posted, .class files will end up in the same location as corresponding .java files
  3. 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)
  4. a makefile typically lives in the root project directory. Often, complex projects will have other makefiles for sub-directories



回答2:


I struggle to remember my make, but I think you need:

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


来源:https://stackoverflow.com/questions/15756399/basics-on-java-makefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!