injoy
injoy

Reputation: 4383

What's the common way to resolve dependencies in Makefile?

I've seen lots of methods been used to resolved the dependencies in Makefile, such as using gcc -MM and sed commond, or using the include directive (plus a little Perl magic), or qmake, or automake, or info make, etc.

Facing such many options, I am confused of which should I choose. So, I wanna know what's the common way to resolve dependencies in Makefile nowadays? What's the best way to cope with this problem?

PS: C/CPP project.

Upvotes: 4

Views: 2307

Answers (3)

Eric Melski
Eric Melski

Reputation: 16830

There are several ways to generate make-compatible dependencies for C/C++ projects:

  • gcc -M, which comes in several flavors and is sort of "the gold standard" in terms of accuracy, since it uses the actual compiler to generate the dependencies, and who would know better how to process #include statements than the compiler itself?
  • makedepend, which is generally discouraged in favor of compiler-generated dependencies.
  • fastdep, another third-party dependency generator which purports to be faster than gcc -M.
  • ElectricAccelerator has a built-in feature called autodep which uses the filesystem usage activity of the commands invoked in the build to generate dependency information. The advantage of autodep over the alternatives is that it is extremely fast and completely tool and programming language independent -- while the others are all tied to C/C++ or require the use of a specific compiler, autodep works with all types of build tools.

I did a performance comparison of several of these options a while back.

Disclaimer: I am the architect and lead developer of ElectricAccelerator.

Upvotes: 0

Jirka Hanika
Jirka Hanika

Reputation: 13547

If you only need to support lots of Linux distributions (as you noted in a comment), then I'd recommend the automake/autoconf suite.

This answer assumes you are only asking generally and you do not yet know what specific issues you will have to resolve as you go.

Edit:

GNU make alone can handle dependency generation within your own project.

autoconf handles optional or alternative dependencies on third party libraries, tools or system features. automake provides macros some of which are occasionally useful even if you are otherwise using autoconf without automake.

A side benefit of starting with automake outright is that your makefiles will behave completely predictably (in terms of conventions and portability) with less investment of attention.

Hence my humble recommendation.

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126536

Generally if all you care about is systems that support GNU make and gcc (such as all linux variants and most unix like systems these days), you just use gcc's various -M flags to generate the dependencies and then -include them in your Makefile. There's some good info in this question -- generally there's no need to use sed or any more complex tools.

Upvotes: 1

Related Questions