zelinka zelinka - 1 year ago 88
C Question

Example makefile for building simple c project recompiling when headers change

Does anyone have a complete makefile that can do the following:

  1. Rebuilds the project if a HEADER file changes

  2. The cpp files are listed in the makefile

  3. The header files are NOT listed in the makefile

  4. The header files are allowed to have different names than the cpp files

  5. Some of the cpp files do not have header files

I have seen instructions for figuring out how to make the make tool figure out header dependencies, but none of these instructions provide anything remotely resembling a working example. A simple example would be as follows: main.cpp C1.cpp C1.h C2.cpp C2.h

Answer Source
CXX = g++

OBJECTS := main.o C1.o C2.o

all: $(OBJECTS)

%.o : %.cpp
    $(CXX) $(CPPFLAGS) -Wall -MMD -c $< -o $@

-include *.d
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download