简体   繁体   English

用makefile编译头文件依赖

[英]compile headers dependencies with makefile

I am programming an UDP client server application in the C programming language; 我正在使用C编程语言编写UDP客户端服务器应用程序。 I want to automatically compile 2 sources files and 3 header files whenever the dependencies change so I decided to use the make utility. 每当依赖项发生更改时,我都希望自动编译2个源文件和3个头文件,因此我决定使用make实用程序。

The makefile target is called "edit" : makefile目标称为“ edit”

    edit : server_UDP.o  client_UDP.o \
            gcc -o edit server_UDP.o  client_UDP.o \


    client_UDP.o : client_UDP.c cliHeader_UDP.h  wrapHeader.h
            gcc -c client_UDP.c

    server_UDP.o : server_UDP.c servHeader_UDP.h  wrapHeader.h
            gcc -c  server_UDP.c

It doesn't trigger a recompile when I change a few lines of code in wrapHeader.h. 当我更改wrapHeader.h中的几行代码时,它不会触发重新编译。

How do to I modify the edit makefile rule(s) when there is a change in wrapHeader.h to recompile server_UDP and client_UDP ? wrapHeader.h中发生更改以重新编译server_UDP和client_UDP时,如何修改edit makefile规则?

**note : wrapHeader.h is the main header **注意 :wrapHeader.h是主要标题

cliHeader_UDP.h : include "wrapHeader.h" cliHeader_UDP.h:包括“ wrapHeader.h”

servHeader_UDP.h : include "wrapHeader.h" servHeader_UDP.h:包括“ wrapHeader.h”

I think what you want are Make dependency files. 我认为您想要的是制作依赖文件。

You can specify the compiler to generate a dependency file for you with the '-MMD -MP' arguments, which create a new file with the same name as the source file except with the extension *.d, in the same folder as your source. 您可以使用-MMD -MP参数指定编译器为您生成依赖文件,该文件将在与源文件相同的文件夹中创建一个与源文件名称相同的新文件,但扩展名为* .d。 。

The dependency file contains all the headers the code depends on, which will lead to GNU make compiling your source file if a header it uses is modified. 依赖文件包含代码依赖的所有标头,如果修改了它使用的标头,这将导致GNU make编译您的源文件。

An example dependency file enabled makefile: 启用依赖文件的示例生成文件:

# Makefile

CC   := gcc
LD   := g++

# The output executable.
BIN   := program

# Toolchain arguments.
CFLAGS    := 
CXXFLAGS  := $(CFLAGS)
LDFLAGS   := 

# Project sources.
C_SOURCE_FILES := mysourcefile1.c src/myothersrc.c
C_OBJECT_FILES := $(patsubst %.c,%.o,$(C_SOURCE_FILES))

# The dependency file names.
DEPS := $(C_OBJECT_FILES:.o=.d)

all: $(BIN)

clean:
    $(RM) $(C_OBJECT_FILES) $(DEPS) $(BIN)

rebuild: clean all

$(BIN): $(C_OBJECT_FILES)
    $(LD) $(C_OBJECT_FILES) $(LDFLAGS) -o $@

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

# Let make read the dependency files and handle them.
-include $(DEPS)

This should work for your situation: 这应该适合您的情况:

SOURCES := server_UDP.c client_UDP.c
OBJECTS := $(patsubst %.c,%.o,$(SOURCES))

DEPS := $(OBJECTS:.o=.d)

edit: $(OBJECTS)
    gcc -o edit $(OBJECTS)

%.o: %.c
    gcc -c $< -o $@

-include $(DEPS)

You did not say that edit.c includes your two specific headers, but I guess it must, if it links to the objects. 您没有说edit.c包括您的两个特定头,但是我想它必须链接到对象。

This is exactly the scenario where makepp plays out one of its strengths: If you follow the convention that for every .o file you need to link there is an include statement of a corresponding name (in your case that would be client_UDP.h & server_UDP.h ) then makepp will figure everything out itself, besides detecting the header files as dependencies. 这正是makepp发挥其优势之一的方案:如果遵循约定,对于每个需要链接的.o文件,都有一个包含相应名称的include语句(在您的情况下为client_UDP.hserver_UDP.h ),那么makepp除了将头文件检测为依赖项之外,还会自行解决所有问题。

This even works recursively, so if you had a wrapHeader.c (where there is no corresponding include statement in edit.c ), that would get automatically compiled and linked. 这甚至工作递归,所以如果你有一个wrapHeader.c (那里没有相应的包括在声明中edit.c ),将得到自动编译和链接。

So you don't need a makefile . 因此,您不需要makefile But if you want to avoid calling makepp edit everytime, then you can create a one-liner 但是,如果您希望避免每次都调用makepp edit ,那么可以创建一个单行

edit:

You will only need to learn make syntax, if you have more complex requirements. 如果您有更复杂的要求,则只需学习make语法。 But if you do, there is no limit. 但是,如果您这样做,则没有限制。 Besides doing almost all that GNU make can, there are lots more useful things, and you can even extend your makefiles with some Perl programming. 除了完成GNU make几乎所有的工作外,还有很多有用的事情,甚至可以使用一些Perl编程来扩展makefile。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM