0

我是 makefile 的新手,我想执行几个操作。

我当前的makefile如下:

OBJECTS=radio_listener.o radio_app_comm_func.o
TARGET_EXECUTABLE=radio_listener

# Default target
all: ${TARGET_EXECUTABLE}

# Compile all required .o files this way
%.o: %.c
    @echo "Compiling $<"
    @gcc -c -g -m32 $< -o $@ 

# Compile target exe by combining all objects
${TARGET_EXECUTABLE}: ${OBJECTS}
    @echo "Linking $@"
    @gcc ${OBJECTS} -g -m32 -o $@

# Cleanup rule
clean:
    @echo "Cleaning up..."
    @rm -f *.o ${TARGET_EXECUTABLE}
    @echo "Done."

.PHONY: clean all

1.假设我执行'make'。然后更改radio_app_comm_ types .h中的 MACRO 定义。

radio_app_comm_types.h 包含在radio_app_comm_func.h中。

更改 MACRO 并再次执行 'make' 后,我得到“对于 'all' 无事可做

显然,我需要以某种方式告诉我的 Makefile,radio_app_comm_func.h 依赖于 radio_app_comm_types.h。

这个怎么做?

2.我感兴趣的第二件事,是如何使用相同的 Makefile,dor 不同的可执行文件。

目前我只有radio_listener,但我计划添加radio_controlradio_server,它们的h 文件还将包括radio_app_comm_func.h,也许还有其他一些 h 文件(以便使用它们的相关 .c 文件。

我怎样才能做到这一点?我希望实现如下流程:

a)执行make,将为每个 exe 编译所有相关文件,最后将为我创建 3 个不同的可执行文件 - radio_listener、radio_control、radio_server

b)执行make server(例如),将编译所有需要radio_server的相关文件(包括寻找更改,比如说,在radio_app_comm_types.h中),最后将只为我创建 1 个可执行文件 - radio_server

c)执行make controller ...编译所有需要的radio_control等(我猜你明白我的意思)

如果相关的话,我正在开发 Ubuntu x64 系统。

任何帮助都感激不尽。

谢谢你。

4

2 回答 2

2

这是一个使用高级自动依赖规则的版本,并允许构建多个目标。更新顶部附近的标志并在其中创建新条目TARGETS并创建一个新xxx_SRC变量以添加新目标。

如果您想要当前目录中的依赖文件,您可以更改DEPDIR为 just.或通过 makefile 并删除对它的引用,无论哪种方式。

TARGETS := exe1 exe2 exe3

DEPDIR = .d

exe1_SRC := foo.c bar.c
exe2_SRC := biz.c boz.c
exe3_SRC := bling.c blang.c

CC       := gcc

CFLAGS   := -g -O2
CPPFLAGS := -DXXX -I../include

LDFLAGS  :=  -L../lib
LDLIBS   :=  -lfoo -lbar

# ----- Nothing below here needs to be changed

.PHONY: all
all : $(TARGETS)

.SECONDEXPANSION:
# For each target, depend on the .o files for its sources
$(TARGETS): $$(patsubst %.c,%.o,$$($$@_SRC))
        $(LINK.c) $^ $(LDLIBS) -o $@

ALLSRC := $(foreach T,$(TARGETS),$($T_SRC))

.PHONY: clean
clean: 
        rm -f $(TARGETS) *.o $(DEPDIR)/*.d

# -----
# Advanced auto-dependency, from:
# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/

$(shell mkdir -p $(DEPDIR) >/dev/null)
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td

COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d

%.o : %.c
%.o : %.c $(DEPDIR)/%.d
        $(COMPILE.c) $(OUTPUT_OPTION) $<
        $(POSTCOMPILE)

$(DEPDIR)/%.d: ;
.PRECIOUS: $(DEPDIR)/%.d

-include $(patsubst %,$(DEPDIR)/%.d,$(basename $(ALLSRC)))
于 2016-01-04T08:16:55.120 回答
0

here is a makefile that incorporates generation of dependencies.

    SHELL = /bin/sh

    target1 := <first executable name>
    target2 := <second executable name>
    target3 := <third executable name>

    #
    # details for target1
    #
    SRC1 := <list of files for target1
    OBJ1 := $(SRC1:.c=.o) 

    #
    # details for target2
    #
    SRC2 := <list of files for target2
    OBJ2 := $(SRC2:.c=.o) 

    #
    # details for target3
    #
    SRC3 := <list of files for target3
    OBJ3 := $(SRC3:.c=.o)

    MAKE    :=  /usr/bin/make

    CC      :=  /usr/bin/gcc

    CP      :=  cp

    MV      := mv

    LDFLAGS :=  

    DEBUG   :=  -ggdb3

    CCFLAGS :=  $(DEBUG) -Wall -Wextra -pedantic -std=c99 -Wconversion

    #CPPFLAGS += =MD

    LIBDIRS :=  -L/usr/lib -L/usr/local/lib
    LIBS    :=  -l<library short name> ...


    .PHONY: all
    all : $(target1)  $(target2) $(target3)


    #
    # link the .o files into the executable 
    # using the linker flags
    # -- explicit rule
    #
    $(target1): $(OBJ1)
        #
        # ======= $@ Link Start =========
        $(CC) $(LDFLAGS) -o $@ $(OBJ1) $(LIBDIRS) $(LIBS)
        # ======= $@ Link Done ==========
        #

    $(target2): $(OBJ2)
        #
        # ======= $@ Link Start =========
        $(CC) $(LDFLAGS) -o $@ $(OBJ2) $(LIBDIRS) $(LIBS)
        # ======= $@ Link Done ==========
        #

    $(target3): $(OBJ3)
        #
        # ======= $@ Link Start =========
        $(CC) $(LDFLAGS) -o $@ $(OBJ3) $(LIBDIRS) $(LIBS)
        # ======= $@ Link Done ==========
        #

    #
    #create dependancy files -- inference rule
    #
    %.d: %.c 
        # 
        # ========= START $< TO $@ =========
        $(CC) -M $(CPPFLAGS) $< > $@.$$$$;                      \
        sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;     \
        rm -f $@.$$$$
        # ========= END $< TO $@ =========

    # 
    # compile the .c files into .o files using the compiler flags
    # -- inference rule
    #
    %.o: %.c %.d 
        # 
        # ========= START $< TO $@ =========
        $(CC) $(CCFLAGS) -c $< -o $@ -I. 
        # ========= END $< TO $@ =========
        # 



    .PHONY: clean
    clean: 
        # ========== CLEANING UP ==========
        rm -f *.o
        rm -f $(target1) $(target2) $(target3)
        rm -f *.d
        # ========== DONE ==========

    # include the contents of all the .d files
    # note: the .d files contain:
    # <filename>.o:<filename>.c plus all the dependencies for that .c file 
    # I.E. the #include'd header files
    # wrap with ifneg... so will not rebuild *.d files when goal is 'clean'
    #
    ifneq "$(MAKECMDGOALS)" "clean"
    -include $(DEP)
    endif
于 2016-01-03T11:19:46.223 回答