歌厅源$ C ​​$ C结构的Makefile [英] Geting source code structure in Makefile

查看:129
本文介绍了歌厅源$ C ​​$ C结构的Makefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个C项目,我决定把源$ C ​​$ C和它在不同的目录对象。根目录下有这样的事情:

  SmartC▶树-L 1

├──建
├──DOC
├──Makefile文件
├──README.md
├──SRC
├──测试
└──垃圾桶

所以,src和内置的目录里面,我把两个人的Makefile做编译和链接工作。

src目录(这里我把源$ C ​​$ C)的结构如下:

  SRC
├──graph.c
├──graph.h
├──list.c
├──list.h
├──的main.c
├──Makefile文件
├──node.c
├──node.h
├──tree.c
├──tree.h中
└──类型
    ├──complex.c里
    ├──complex.h
    ├──matrix.c
    └──matrix.h

和内置有相同的结构,但它的目的是为存储由编译的所有对象。

我的问题是关于我的src / Makefile文件:

  BINDIR = ../built/srcCC = GCCCFLAGS = -g -Wall -O3物体= \\
          $(BINDIR)/main.o \\
          $(BINDIR)/node.o \\
          $(BINDIR)/list.o \\
          $(BINDIR)/graph.o \\
          $(BINDIR)/tree.o \\
          $(BINDIR)/types/complex.o \\
          $(BINDIR)/types/matrix.o \\
编译:$(对象)$(BINDIR)/%○:%.C
    $(CC)-c $< -o $ @ $(CFLAGS)

这Makefile中创建源$ C ​​$ C的所有对象,src目录里面,并将其移动到内置/ src目录。但是,每次我创建一个新的源$ C ​​$ C文件(* .c)中,我已经把它的对象的名字在这个makefile,因此它可以被编译。我希望做一个自动搜索,在src目录中,并填写物的变量与此搜索。

是任何人有如何做到这一点的一些想法?我的意思是,一个特定的目录内源$ C ​​$ C自动搜索?

我甚至接受任何其他的战略,而不是我在做什么。

===========回答===============

我得到了有关通配符的尖端(在评论)。所以我做了。这里是我找到了解决办法。

的src / Makefile文件

  BINDIR = ../built/srcCC = GCCCFLAGS = -g -Wall -O3
对象:= $(。patsubst%.C,$(BINDIR)/%0,$(通配符的* .c * / * C))编译:$(对象)$(BINDIR)/%○:%.C
    $(CC)-c $< -o $ @ $(CFLAGS)


解决方案

EDIT [解决]

我想做到以下几点。

创建变量到项目的每个目录

  SRCDIR = SRC
OBJDIR = OBJ
LIBDIR = LIB
DOCDIR = DOC
HDRDIR =包括CFLAGS = -g -Wall -O3

只得到SRCDIR递归的内部结构

 结构:= $(壳找到$(SRCDIR)型D)

获取所有文件里面的结构变量

  codeFILES:= $(addsuffix / *,$(结构))
codeFILES:= $($通配符(codeFILES))

过滤出特定文件

 #仅过滤特定文件
SRCFILES:= $(过滤%.C,$(codeFILES))
HDRFILES:= $(过滤%.H,$(codeFILES))
OBJFILES:= $(SUBST $(SRCDIR),$(OBJDIR),$(SRCFILES:%C =‰))#筛选出库的主要功能
LIBDEPS:= $(过滤出$(OBJDIR)/main.o,$(OBJFILES))

现在是时候创建规则

 编译:$(OBJFILES)$(OBJDIR)/%○:$(添加preFIX $(SRCDIR)/,%C%.H)
    $(CC)-c $< -o $ @ $(CFLAGS)

通过这种方法,你可以看到,我使用的是结构变量只拿到了SRCDIR目录中的文件,但它可以用于其他用途,以及像镜子里面OBJDIR的SRCDIR只有一次的结构存储内部子目录。它就像干净的操作之后非常有用:

 清理:
    -rm -r $(OBJDIR)/ *

请注意:编译规则只适用好,如果每个* .C有​​相应的* .h文件(使用相同的基本名称,我的意思)

I'm working on a C project, and I decided to put the source code and its objects in different directories. The root directory has something like that:

SmartC  ▶ tree -L 1
.
├── built
├── doc
├── Makefile
├── README.md
├── src
├── tests
└── trash

So, inside both src and built directories, I put two others Makefiles to do the compile and link jobs.

The src directory (where I put the source code) has the following structure:

src
├── graph.c
├── graph.h
├── list.c
├── list.h
├── main.c
├── Makefile
├── node.c
├── node.h
├── tree.c
├── tree.h
└── types
    ├── complex.c
    ├── complex.h
    ├── matrix.c
    └── matrix.h

and the built has the same structure, but it is intended to store all objects made by compilation.

My question is about my src/Makefile:

BINDIR = ../built/src                

CC = gcc

CFLAGS = -g -Wall -O3

OBJECTS = \
          $(BINDIR)/main.o          \
          $(BINDIR)/node.o          \
          $(BINDIR)/list.o          \
          $(BINDIR)/graph.o         \
          $(BINDIR)/tree.o          \
          $(BINDIR)/types/complex.o \
          $(BINDIR)/types/matrix.o  \


compile: $(OBJECTS)

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

This Makefile creates all the objects of the source code, inside src directory, and move them to built/src. But, every time I create a new source code file (*.c), I have to put the name of its object in this makefile, so it can be compiled. I'd like to do an automatic search, inside the src directory, and fill the "OBJECTS" variable with this search.

Is anyone has some idea of how to accomplish this? I mean, automatic search for source code inside an specific directory?

I even accept any other strategy rather than what I'm making.

=========== Answer ===============

I got the tip (in comments) about wildcards. So I did. Here is the solution I found.

src/Makefile

BINDIR = ../built/src                                         

CC = gcc                                                      

CFLAGS = -g -Wall -O3                                         


OBJECTS := $(patsubst %.c,$(BINDIR)/%.o,$(wildcard *.c */*.c))

compile: $(OBJECTS)                                           

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

解决方案

EDIT [Solved]

I like to do the following.

Create Variables to Each Directory of the Project

SRCDIR = src                                                           
OBJDIR = obj
LIBDIR = lib
DOCDIR = doc
HDRDIR = include

CFLAGS = -g -Wall -O3

Get Only the Internal Structure of SRCDIR Recursively

STRUCTURE := $(shell find $(SRCDIR) -type d)     

Get All Files inside the STRUCTURE Variable

CODEFILES := $(addsuffix /*,$(STRUCTURE))
CODEFILES := $(wildcard $(CODEFILES))            

Filter Out Only Specific Files

# Filter Only Specific Files                                
SRCFILES := $(filter %.c,$(CODEFILES))
HDRFILES := $(filter %.h,$(CODEFILES))
OBJFILES := $(subst $(SRCDIR),$(OBJDIR),$(SRCFILES:%.c=%.o))

# Filter Out Function main for Libraries
LIBDEPS := $(filter-out $(OBJDIR)/main.o,$(OBJFILES))

Now it is Time to create the Rules

compile: $(OBJFILES)

$(OBJDIR)/%.o: $(addprefix $(SRCDIR)/,%.c %.h)
    $(CC) -c $< -o $@ $(CFLAGS) 

With this approach, you can see that I'm using the STRUCTURE variable only to get the files inside the SRCDIR directory, but it can be used for others purposes as well, like mirror the SRCDIR inside OBJDIR once STRUCTURE stores only the internal sub-directories. It is quite useful after clean operations like:

clean:
    -rm -r $(OBJDIR)/*

NOTE: The compile rule only works well if for each *.c there is the corresponding *.h file (with the same base name, I mean).

这篇关于歌厅源$ C ​​$ C结构的Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆