Makefile同时编译C和Java程序

发布于 2024-09-01 14:22:25 字数 550 浏览 4 评论 0原文

我有三个程序需要同时编译,2个用C编写,1个用java编写。当它们在 C 中时,我让所有三个 Makefile 一起工作,但后来用 java 重写了其中一个......有没有一种方法可以使用相同的 makefile 一次编译所有 3 个?

这是我当前的 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-09-08 14:22:25

是的,您可以一次性编译它们。如果您的“all”目标依赖于所有三个应用程序,那么“make all”应该构建所有这些应用程序。您可以添加“-j3”以使用三个单独的线程和/或进程进行实际编译(不清楚“一次”的含义)。另外,这里还有一些批评:

  • 不要定义“CC”、“CFLAGS”或“LDFLAGS”。您永远不应该定义“CC”,因为它是自动为您定义到系统上的默认 C 编译器的,并且您应该仅根据需要附加“CFLAGS”和“LDFLAGS”(使用 +=),而不是破坏它们,就像简单的那样分配给它们会使您的 Makefile 变得不灵活(因为它们无法从外部覆盖或扩展)。
  • 使用 CLASSES 引用“.java”文件非常令人困惑......您可能应该将变量重命名为 JAVA_SRCS,并定义 JAVA_CLASSES=${JAVA_SRCS:.java=.class}。

有关更多说明,请参阅我的 Makefile 教程< /a>.也就是说,您可能需要考虑更现代的构建系统,例如 BazelGradle。这些系统被设计为更易于使用、更不易出错、更便携(因为它们使便携做事变得更容易)并且速度更快。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文