要编译两个文件,我已经创build了一个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
现在,如果我的项目包含文件夹包含子文件夹&他们包含更多的文件。 那我可以编写模式规则来编译每个文件并创build一个通用的可执行文件吗?
1>我将不得不在每个子文件夹中创buildmakefile,以便可以从主makefile调用makefile,例如将静态驱动程序集成到linux内核,每个驱动程序都有相应的makefile?
2>或完整项目的常见makefile?
3>我可以使用patsubst来编译每个文件,而不用提到那里的名字。
4>我怎么能结合每个* .o来创build名为main
可执行文件。
编辑:—
@Jan Hudec我已经修改我的生成文件,根据您的评论(我已经在上面贴出)。 现在我只是想在我的主文件夹中的两个文件夹。 我遇到以下错误文件夹结构: –
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和2:文件名可以安全地包含目录和%
匹配/
根据需要。 所以你可以很容易地有:
$(wildcard subdir/*.c) $(wildcard anotherdir/*.c)
甚至
$(wildcard */*.c)
…或如keltar在评论中所建议的那样
$(shell find . -name '*.c')
这是递归的。
广告3:你在做。
广告4:使用$(OBJ)
创建一个目标作为依赖关系,并使用自动变量,就像编译时一样:
main : $(OBJ) $(LD) $(LDFLAGS) -o $@ $< $(LIBS)