简体   繁体   中英

Makefile to compile both C and Java programs at the same time

I have three programs that need to be compiled at the same time, 2 written in C and 1 in java. I had all three working with the Makefile when they were in C, but then rewrote one of them in java... is there a way to compile all 3 at once with the same makefile?

Here is my current Makefile:

CC=gcc 
JC=javac 
JFLAGS= -g
CFLAGS= -Wall -g -std=c99
LDFLAGS= -lm
.SUFFIXES: .java .class
.java.class:
 $(JC) $(JFLAGS) $*.java

CLASSES = kasiski.java kentry.java

ALL= ic ftable kasiski

all: $(ALL)

ic: ic.o

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

ftable: ftable.o

ic.o: ic.c ic.h

ftable.o: ftable.c ftable.h

.PHONY: clean

clean:
 rm -rf core* *.class *.o *.gch $(ALL)

Yes, you can compile them all at once. If your "all" target depends on all three applications, then "make all" should build all of them. You can throw in "-j3" to actually compile using three separate threads and/or processes (it isn't clear what you mean by "at once"). Also, a couple criticisms here:

  • Do not define "CC", "CFLAGS", or "LDFLAGS". You should never define "CC" as it is automatically defined for you to the default C compiler on the system, and you should merely append to "CFLAGS" and "LDFLAGS" as needed (using +=) instead of clobbering them, as simply assigning to them makes your Makefile inflexible (because they cannot be overrided or augmented externally).
  • Using CLASSES to refer to ".java" files is confusing as hell... you should probably rename the variable to JAVA_SRCS, and define JAVA_CLASSES=${JAVA_SRCS:.java=.class}.

For more explanation, please see my Makefile tutorial . That said, you may want to consider a more modern build system such as Bazel or Gradle . These systems are designed to be much simpler to use, less error prone, more portable (because they make it easier to do things portably), and faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM