complie .h file

how can i include a re-compilation/update for my own ".h" files within my makefile.

This is my makefile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
COMPILER = g++
SOURCES =  main.cpp 
APP = executable/main

OBJECTS = $(SOURCES:.cpp=.o)

all: $(SOURCES) $(APP) 
	
$(APP): $(OBJECTS)
	$(COMPILER) $(OBJECTS) -o $@

clean:
	rm -rf *.o $(APP)


suppose now i want to re-compile the project but i just modified whatever.h and whatever1.h. these files are included in the header of main.cpp.

Thanks in Advance

BR,

Ewa
Hi!

i solved it here the solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
COMPILER = g++
SOURCES =  main.cpp
HEADERS = whatever.h whatever1.h
APP = executable/main

OBJECTS = $(SOURCES:.cpp=.o)

all: $(SOURCES) $(APP) 

$(APP): $(OBJECTS) $(HEADERS)
    $(COMPILER) $(OBJECTS) -o $@

clean:
    rm -rf *.o $(APP)


Thanks for your time and showing concern.

Regards
:-) For a horrible moment, the title of this thread made me think you were trying to compile the actual header. But it turned out you were just wanting to sort out your dependencies!

While your solution will work for your specific case, it's actually missing a level of dependency. The .o files depends on the .cpp file, and the .cpp files depend on the header files.

If main.cpp includes whatever.h and whatever1.h, and other.cpp includes whatever.h and other.h, the following makefile ensures that changes to whatever.h triggers a full rebuild, but changes to whatever1.h or other.h just trigger the rebuild of a single .cpp file plus a relink. Not a big deal for two files, but with bigger projects...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
COMPILER = g++
SOURCES =  main.cpp other.cpp
APP = executable/main

OBJECTS = $(SOURCES:.cpp=.o)

all: $(SOURCES) $(APP) 

$(APP): $(OBJECTS)
    $(COMPILER) $(OBJECTS) -o $@

main.cpp : whatever.h whatever1.h

other.cpp : whatever.h other.h

clean:
    rm -rf *.o $(APP) 


Note that with more complicated projects, you can use a tool to generate the header dependencies, like autodep (as kbw mentioned) or makedepend.
Last edited on
Try the -MD flag:

Try something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
COMPILER  = g++
SOURCES = main.cpp other.cpp
OBJECTS = $(SOURCES:.cpp=.o)
DEPS = $(SOURCES:.cpp=.d)
APP = executable/main

all: $(APP)


$(APP) : $(OBJECTS)
	mkdir -p executable
	$(COMPILER) $(OBJECTS) -o $@

clean:
	rm -fr *.o $(APP) *.d executable

%.o : %.cpp
	g++ -MMD -c -o $@ $<

-include $(DEPS)


Don't forget the include statement at the end.

The -MD flag calculates all of the dependencies you need and stores them in the dependency files. Including the dependency files gives you all the lines like main.cpp : whatever.h whatever1.h automatically

Last edited on
Hi Guys !

thanks for showing concern, your help is appreciated and was really fruitful

Regards,

Ewa
Topic archived. No new replies allowed.