1

I have 3 files

main.cpp
a.h
b.h

main.cpp includes both a.h and b.h b.h includes a.h

could anyone explain me how should I write a make file for this?

Is this correct?

objects = main.o 
sources = main.cpp

myProj: $(objects) 
    g++ -o myProj $(objects)

main.o: a.h b.h 


$(objects): $(sources)
    g++ -c $(sources)

clean:
    rm $(objects) myProj

I dont know how to specify the dependency of b.h on a.h

2 Answers 2

2

Since headers are always compiled as part of .c/.cpp file, there is no need to specify header-to-header dependency. The dependency that you have specified already is sufficient, because main.cpp will recompile when a.h and/or b.h change.

Sign up to request clarification or add additional context in comments.

3 Comments

so my make file is correcct??
@user1344389 Yes, I think it's correct. You can check that the dependencies are OK by modifying one or both headers, and checking if it triggers a recompile; it should.
@user1344389 It might be educational to read about Automatic Variables to improve your makefile. For example, in stead of g++ -o myProj $(objects) it would be better to use g++ -o $@ $^
1

If you are at all any more confused on makefile concepts, I would recommend checking out this helpful tutorial.

http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/makefile.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.