make for compiling - 文件夹中的所有* .c文件&项目中的子文件夹

时间:2013-10-23 10:40:28

标签: linux makefile

要编译两个文件,我创建了一个makefile,我用它来提及对象名称,或者我可以使用patsubst使用模式规则。

# ----------------------------------------------------------------------------
# Makefile for building tapp
#
# Copyright 2010 FriendlyARM (http://www.arm9.net/)
#

ifndef DESTDIR
DESTDIR            ?= /opt/FriendlyARM/tiny6410/linux/rootfs_qtopia_qt4
endif

#CFLAGS              = -c -Wall -O2  # wall is for warning show and 02 is optiminisation level 2
CFLAGS              = -c -O2  # wall is for warning show and 02 is optiminisation level 2
#CC                    = arm-linux-gcc   # compiler name
CC                    = gcc   # compiler name
LD                    = ld

INSTALL             = install         # 

TARGET              = led_player_project

#OBJ = led-player_backup.o led-player.o
OBJ := $(patsubst %.c,%.o,$(wildcard *.c */*.c))

#OBJ = $(shell find . -name '*.c')

all: $(TARGET) 
#all: $(OBJ)

led_player_project : $(OBJ)
    $(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
#       $(LD) $(LDFLAGS) -o $@ $< $(LIBS)

%.o : %.c
    $(CC) $(CFLAGS) $< -o $@

#$< -o $@


install: $(TARGET)
    $(INSTALL) $^ $(DESTDIR)/usr/bin

clean :
    rm -rf *.o $(TARGET) $(OBJ)


# ----------------------------------------------------------------------------

.PHONY: $(PHONY) install clean

# End of file
# vim: syntax=make

#EOF

现在,如果我的项目包含文件夹包含子文件夹&amp;它们包含更多文件。然后我可以编写模式规则来编译每个文件&amp;创建一个通用的可执

1&GT;我是否必须在每个子文件夹中创建makefile,以便我可以从主makefile调用该makefile,比如将静态驱动程序集成到linux内核,每个驱动程序都有各自的makefile?
2 - ;或完整项目的常见makefile?
3 GT;我可以使用patsubst编译每个文件而不提及名称 4&GT;如何组合每个* .o来创建名为main的可执行文件。

enter image description here

编辑:---
@Jan Hudec 我根据你的评论修改了我的makefile(我在上面发布了它)。现在我只是尝试在我的主文件夹中的两个文件夹。我收到了以下错误 文件夹结构: -

main Folder  ----> one Folder 
             ----> two Folder 

文件夹主要包含: -

main.c 
main.h
Makefile

文件夹一包含: -

one.c
one.h

文件夹二包含: -

two.c
two.h

main.c内容: -

#include <stdio.h>
#include <stdlib.h>

#include "main.h"

int main()
{
  char *p;

  printf("\n\n main \n");

  one();
  two();

  return 0;
}

main.h内容:---

#include "one/one.h"
#include "two/two.h"

one.c内容:---

    #include <stdio.h>
    #include <stdlib.h>

#include "one.h"

    void one()
    {

      printf("\n one \n");

    }

one.h内容:---

void one();

two.c内容:---

#include <stdio.h>
#include <stdlib.h>

#include "two.h"

void two()
{

  printf("\n two \n");

}

two.h内容:---

void two();

我在制作时遇到错误:----

ignite@ignite:~/testing/main$ make
gcc    -c -O2   main.c -o main.o
gcc    -c -O2   one/one.c -o one/one.o
gcc    -c -O2   two/two.c -o two/two.o
ld  -o led_player_project main.o one/one.o two/two.o 
ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080
main.o: In function `main':
main.c:(.text.startup+0x11): undefined reference to `puts'
one/one.o: In function `one':
one.c:(.text+0xb): undefined reference to `puts'
two/two.o: In function `two':
two.c:(.text+0xb): undefined reference to `puts'
make: *** [led_player_project] Error 1
ignite@ignite:~/testing/main$ 

1 个答案:

答案 0 :(得分:5)

广告1和2:文件名可以安全地包含目录,并根据需要%匹配/。所以你可以轻松拥有:

$(wildcard subdir/*.c) $(wildcard anotherdir/*.c)

甚至

$(wildcard */*.c)

......或者如评论中的keltar所建议的那样

$(shell find . -name '*.c')

这是递归的。

广告3:你正在这样做。

广告4:使用$(OBJ)作为依赖项创建目标,并像编译一样使用自动变量:

main : $(OBJ)
        $(LD) $(LDFLAGS) -o $@ $< $(LIBS)