views:

157

answers:

1

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 switched one of them to 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)
+2  A: 

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, if these are really three separate applications, then they probably deserve their own, separate projects, with separate build systems. You can see the C++ Project Template and Java Project Template for complete project directory infrastructure that can easily be reused for any C++ or Java application.

Michael Aaron Safyan
Removing the "CC" line solved the problem... although I also had files "kasiski.c" and "kasiski.h" (from a previous C attempt at the project) which caused a lot of noise and errors from make (I still can't figure out why). After deleting those source files, it compiled everything without a hitch. Thanks!