创建遍历目录的makefile

标签 c gcc makefile

                         $project
                            |
     +------------+----------------+----------------+
     |            |                |                |
   gestore/     tipoA/           tipoB/            shared/
     |            |                |                 |
     +            +                +                 +
     |            |                |        +--------|------------------+
  gestore.c     tipoA.c          tipoB.c/   funzioni.c/.h sem.c/.h gF.c./ 

你好,我想为这个项目创建一个 makefile,它执行这些命令:

共享文件夹中的第 1 步:

gcc -c -o gestioneFile gestioneFile.c
gcc -c -o sem sem.c
gcc -c -o funzioni funzioni.c

tipoA 文件夹中的第 2 步:

gcc -o tipoA tipoA.c ../shared/funzioni ../shared/sem ../shared/gestioneFile

tipoB 文件夹中的第 3 步:

gcc -o tipoB tipoB.c ../shared/funzioni ../shared/sem ../shared/gestioneFile

gestore 文件夹中的第 4 步:

gcc -o gestore gestore.c ../shared/funzioni ../shared/sem ../shared/gestioneFile

./gestore

这是我的 makefile,但它不起作用:

#Makefile

cc=gcc

./gestore/gestore: ./shared/gestioneFile.o ./shared/sem.o ./shared/funzioni.o ./tipoA/tipoA.o ./tipoB/tipoB.o ./gestore/gestore.o
    ./gestore/gestore

./shared/gestioneFile.o: 
    cc -c -o ./shared/gestioneFile ./shared/gestioneFile.c

./shared/sem.o:
    cc -c -o ./shared/sem ./shared/sem.c

./shared/functions.o: 
    cc -c -o ./shared/funzioni ./shared/funzioni.c

./tipoA/tipoA.o: ./shared/gestioneFile.o ./shared/sem.o ./shared/funzioni.o 
    cc -o ./tipoA/tipoA ./tipoA/tipoA.c ./shared/funzioni ./shared/sem ./shared/gestioneFile 

./tipoB/tipoB.o: ./shared/gestioneFile.o ./shared/sem.o ./shared/funzioni.o 
    cc -o ./tipoB/tipoB ./tipoB/tipoB.c ../shared/funzioni ../shared/sem ../shared/gestioneFile

./gestore/gestore.o: ./shared/gestioneFile.o ./shared/sem.o ./shared/funzioni.o 
    cc -o ./gestore/gestore ./gestore/gestore.c ./shared/funzioni ./shared/sem ./shared/gestioneFile

编辑: @HardcoreHenry 现在这是我的 makefile,但它给出了错误:***** Missing separator.**

#Makefile

cc=gcc

./gestore/gestore:  ./shared/gestioneFile.o ./shared/sem.o ./shared/funzioni.o ./tipoA/tipoA.o ./tipoB/tipoB.o ./gestore/gestore.o
    ./gestore/gestore

./shared/gestioneFile.o: 
    cc -c -o ./shared/gestioneFile.o ./shared/gestioneFile.c

./shared/sem.o:
    cc -c -o ./shared/sem.o ./shared/sem.c

./shared/funzioni.o: 
    cc -c -o ./shared/funzioni.o ./shared/funzioni.c

./tipoA/tipoA.o: ./shared/gestioneFile.o ./shared/sem.o ./shared/funzioni.o 
    cc -o ./tipoA/tipoA.o ./tipoA/tipoA.c ./shared/funzioni.o ./shared/sem.o ./shared/gestioneFile.o

./tipoB/tipoB.o: ./shared/gestioneFile.o ./shared/sem.o ./shared/funzioni.o 
    cc -o ./tipoB/tipoB.o ./tipoB/tipoB.c ../shared/funzioni.o ../shared/sem.o ../shared/gestioneFile.o

./gestore/gestore.o: ./shared/gestioneFile.o ./shared/sem.o ./shared/funzioni.o 
    cc -o ./gestore/gestore.o ./gestore/gestore.c ./shared/funzioni.o ./shared/sem.o ./shared/gestioneFile.o

已解决(即使第一条规则不执行文件):

./gestore/gestore: ./shared/gestioneFile.o  ./shared/sem.o  ./shared/funzioni.o ./tipoA/tipoA   ./tipoB/tipoB   ./gestore/gestore
    ./gestore/gestore

./shared/gestioneFile.o:
    gcc -c -o ./shared/gestioneFile ./shared/gestioneFile.c

./shared/sem.o:
    gcc -c -o ./shared/sem ./shared/sem.c

./shared/funzioni.o: 
    gcc -c -o ./shared/funzioni ./shared/funzioni.c

./tipoA/tipoA:  ./shared/gestioneFile.o ./shared/sem.o  ./shared/funzioni.o 
    gcc -o ./tipoA/tipoA ./tipoA/tipoA.c ./shared/gestioneFile ./shared/sem ./shared/funzioni 

./tipoB/tipoB: ./shared/gestioneFile.o  ./shared/sem.o  ./shared/funzioni.o 
    gcc -o ./tipoB/tipoB ./tipoB/tipoB.c ./shared/gestioneFile ./shared/sem ./shared/funzioni

./gestore/gestore: ./shared/gestioneFile.o  ./shared/sem.o  ./shared/funzioni.o 
    gcc -o ./gestore/gestore ./gestore/gestore.c ./shared/gestioneFile ./shared/sem ./shared/funzioni

最佳答案

以下行会给你这些错误:

./tipoA/tipoA.o: ./shared/gestioneFile.o ./shared/sem.o ./shared/funzioni.o 
    cc -o ./tipoA/tipoA ./tipoA/tipoA.c ./shared/funzioni ./shared/sem ./shared/gestioneFile 

首先,您要输出到 ./tipoA/tipoA 而不是 @Nonyme 指出的 ./tipoA/tipoA.o。不过,这不会导致您的编译失败。

导致失败的原因是它试图使用 ./shared/funzioni 作为输入——该文件不存在。 (注意 makefile 目标依赖于 ./shared/funzioni.o,它必须在调用此配方之前构建,但它不会生成 shared/funzioni(没有.o)

您需要将 .o 添加到 cc 命令的目标和源中。

我还建议使用 $(CC),并查找 automatic variables -- 特别是 $@$^ 用于您的食谱。这样更简洁,更不容易出错。

./tipoA/tipoA.o: ./shared/gestioneFile.o ./shared/sem.o ./shared/funzioni.o 
    $(CC) -o $@ $^

关于创建遍历目录的makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47911555/

相关文章:

c - 当我们改变指针的内容时指针指向哪里?

objective-c - 将 char[] 写入 NSOutputStream 并从 NSInputStream 读取

gcc - 哪里有创建 GCC 前端的好文档?

android - 为 Android 构建内核 : multiple target patterns

c++ - 如何链接到从源代码而不是系统库构建的 libusb?

c++ - 如何用类实例成员函数指针填充一个函数指针成员?

c - 在 ARM C 调用约定中要保存哪些寄存器?

windows - 如何使用 sys/ioctl.h 将代码移植到 MinGW gcc?

c - 如何获取调用函数的名称?

make 中的 Shell 状态代码