4

I am programming an UDP client server application in the C programming language; I want to automatically compile 2 sources files and 3 header files whenever the dependencies change so I decided to use the make utility.

The makefile target is called "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.

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 ?

**note : wrapHeader.h is the main header

cliHeader_UDP.h : include "wrapHeader.h"

servHeader_UDP.h : include "wrapHeader.h"

Michaelangel007
  • 2,798
  • 1
  • 25
  • 23
zak
  • 104
  • 1
  • 6
  • 16
  • 1
    Both `gcc` and Gnu `make` have a *fantastic* feature set that, when setup to work in concert with each other, will do precisely what you're looking for. The `gcc -MM` compiler switch will *almost* generate a `make`-compatible target that for you. For information about how this is done, and some sample code to try out, see the online docs of gnu make [at this website](http://www.gnu.org/software/make/manual/html_node/index.html), with specific attention paid to [this section](http://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html#Automatic-Prerequisites). – WhozCraig Apr 07 '13 at 01:28
  • Possible duplicate of [Makefile, header dependencies](http://stackoverflow.com/questions/2394609/makefile-header-dependencies) – Ciro Santilli OurBigBook.com Dec 20 '16 at 00:02

2 Answers2

2

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.

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.

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)
Dylan
  • 572
  • 5
  • 10
  • mmmm i browsed the useful links but i didnt get it clearly,because iam new to makefile utility so Dylan McKay maybe an example on my edit file how to put the code to understand more ,coz i confused ... – zak Apr 07 '13 at 01:53
  • but i want to understand more about makefile , what is patsubst and what is OBJECTS := $(patsubst %.c,%.o,$(SOURCES)) DEPS := $(OBJECTS:.o=.d) – zak Apr 07 '13 at 02:00
  • patsubst is a function provided by GNU make that replaces a pattern with another pattern. When you call $(patsubst %.c,%.o,$(SOURCES)), you are essentially replacing the .c extension with .o in every file that is in the "SOURCES" variable – Dylan Apr 07 '13 at 03:03
  • the deps thing basically does the same, in fact you could make a statement that did the same thing as DEPS := $(OBJECTS:.o=.d) but with patsubst. The $(OBJECTS:.o=.d) expression just replaces ".o" with ".d" in the "OBJECTS" variable – Dylan Apr 07 '13 at 03:05
  • thanks for ur help but its still does not work as i want .when i change the wrapperHeader nothing changed,may i send u the file to test the file i tried it yesterday about 3H:30m but still the same result... it will be great favor for me plz..... – zak Apr 07 '13 at 11:16
  • Seriously sir. look at how `sed` is used in conjunction with -MM to make a multi-target rule of `file.o file.d : file.c`. [in this sample of the gnu make docs](http://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html#Automatic-Prerequisites). This is important as both the .o and the dependency .d file will be rebuilt if the .c changes, and once the .d file is built any future updates to the headers within it will trigger a new build of the .o. Further, adding a new `#include` to the .c regenerates the .d as well. I've used this extensively and it just-plain-rocks. – WhozCraig Apr 09 '13 at 06:27
0

You did not say that edit.c includes your two specific headers, but I guess it must, if it links to the objects.

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.

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.

So you don't need a makefile. But if you want to avoid calling makepp edit everytime, then you can create a one-liner

edit:

You will only need to learn make syntax, if you have more complex requirements. 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.

Daniel
  • 521
  • 1
  • 4
  • 13
  • no edit is the file for makefile commands and client_UDP and server_udp not a Headers ..... its souce codes – zak Apr 07 '13 at 11:18
  • Sorry I don't understand what you are saying. If you want go to our [sourceforge forums](http://sourceforge.net/p/makepp/discussion/) and we can discuss more. There you can also write in Esperanto, German or French if you are more comfortable with one of those. – Daniel Apr 10 '13 at 21:04