为了编译两个文件,我创建了一个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
Run Code Online (Sandbox Code Playgroud)
现在,如果我的项目包含文件夹,则包含子文件夹,并且它们包含其他文件。然后,我可以编写模式规则来编译每个文件并创建通用可执行文件吗?
1>我是否必须在每个子文件夹中创建makefile,以便可以从主makefile中调用该makefile,例如将静态驱动程序集成到linux内核中,每个驱动程序都有各自的makefile吗?
2>还是整个项目的通用Makefile?
3>我可以使用patsubst来编译每个文件而不提及那里的名称吗?
4>如何结合每个* .o在名为的可执行文件上创建main
。
编辑:---
@Jan Hudec我已经根据您的评论修改了我的makefile(我已经在上面发布了)。现在我只是在我的主文件夹中尝试两个文件夹。我收到以下错误文件夹结构:-
main Folder ----> one Folder
----> two Folder
Run Code Online (Sandbox Code Playgroud)
文件夹主包含:-
main.c
main.h
Makefile
Run Code Online (Sandbox Code Playgroud)
文件夹一包含:-
one.c
one.h
Run Code Online (Sandbox Code Playgroud)
文件夹二包含:-
two.c
two.h
Run Code Online (Sandbox Code Playgroud)
main.c内容:-
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
int main()
{
char *p;
printf("\n\n main \n");
one();
two();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
main.h内容:---
#include "one/one.h"
#include "two/two.h"
Run Code Online (Sandbox Code Playgroud)
one.c内容:---
#include <stdio.h>
#include <stdlib.h>
#include "one.h"
void one()
{
printf("\n one \n");
}
Run Code Online (Sandbox Code Playgroud)
一个.h内容:-
void one();
Run Code Online (Sandbox Code Playgroud)
two.c内容:---
#include <stdio.h>
#include <stdlib.h>
#include "two.h"
void two()
{
printf("\n two \n");
}
Run Code Online (Sandbox Code Playgroud)
two.h内容:---
void two();
Run Code Online (Sandbox Code Playgroud)
我在make time时遇到错误:----
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$
Run Code Online (Sandbox Code Playgroud)
广告1和2:文件名可以根据需要安全地包含目录和%
匹配项/
。因此,您可以轻松拥有:
$(wildcard subdir/*.c) $(wildcard anotherdir/*.c)
Run Code Online (Sandbox Code Playgroud)
甚至
$(wildcard */*.c)
Run Code Online (Sandbox Code Playgroud)
...或keltar在评论中建议
$(shell find . -name '*.c')
Run Code Online (Sandbox Code Playgroud)
这是递归的。
广告3:您正在这样做。
广告4:创建具有$(OBJ)
依赖关系的目标,并像编译时一样使用自动变量:
main : $(OBJ)
$(LD) $(LDFLAGS) -o $@ $< $(LIBS)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11171 次 |
最近记录: |