c - 在一个 makefile 中制作两个 C 程序

标签 c unix gcc makefile

是否可以在一个 makefile 中创建两个文件?本质上是一个程序和一个programtest。我看过其他答案,但它们的语法完全超出了我的理解范围。现在我的 Makefile 只生成其中一个程序,我不知道如何让它同时生成两个程序

有人可以提供一个模板来说明如何构建 Makefile 来编译两个程序吗?

最佳答案

    all: main test

    test: objects/Math.o objects/Stack.o objects/Queue.o objects/myUnitTesting.o objects/test.o
            gcc objects/test.o objects/Math.o objects/Stack.o objects/Queue.o objects/myUnitTesting -o test

    main: objects/Stack.o objects/Queue.o objects/Math.o objects/Point.o objects/main.o
            gcc objects/main.o objects/Stack.o objects/Queue.o objects/Point.o objects/Math.o -o main

    objects/test.o: test.c
            gcc -g -Wall -O -c -o objects/test.o test.c

    objects/main.o: main.c
            gcc -g -Wall -O -c -o objects/main.o main.c

    objects/myUnitTesting.o: cs/myUnitTesting.c
            gcc -g -Wall -O -c -o objects/myUnitTesting cs/myUnitTesting.c

    objects/Math.o: cs/Math.c
            gcc -g -Wall -O -c -o objects/Math.o cs/Math.c

    objects/Stack.o: cs/Stack.c
            gcc -g -Wall -O -c -o objects/Stack.o cs/Stack.c

    objects/Queue.o: cs/Queue.c
            gcc -g -Wall -O -c -o objects/Queue.o cs/Queue.c

    objects/Point.o: cs/Point.c
            gcc -g -Wall -O -c -o objects/Point.o cs/Point.c

    clean:
            rm -f objects/*o main

那么你只需要输入:

    make all

它将编译您的 main.ctest.c 文件

关于c - 在一个 makefile 中制作两个 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795910/

相关文章:

linux - Linux-Unix 中的中文/日文用户名

objective-c - 在简单的 perl 或简单的 unix 中分解 JSON 字符串?

c++ - 段错误 - 使用 getopt 时发生核心转储错误

c++ - 除了更改环境变量 "PATH"之外,您还需要做什么才能让 Eclipse 识别 MinGW ?

c - 在 C 中使用标记串联访问和修改变量

c - gcc 内联汇编的奇怪行为?

c - 如何在 c 中分配大容量(GB 数量级)的内存?

linux - 如何对已在后台运行的进程使用 nohup

c++ - GCC 或 Clang "moving target"是否有任何 `-std=` 别名值,暗示 "use the latest standard"?

c++ - 为什么虚拟析构函数写入内存?