Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating nested makefile

I am learning makefiles and I know how to create a simple makefile. I am moving on to nested makefiles. Here is my directory structure

/src
...makefile
...main.cpp
...foo
......makefile
......foo.cpp
......foo.h

When root makefile is called, it calls the makefile in directory foo. Here are my questions

  1. Which makefile should I use to write code for linking all object files? If it is in the root makefile, do I need to specify all object file names there?
  2. Is this nested makefiles a best practice? Or is it good to have only one makefile which is at root?

Any help would be great!

like image 639
Navaneeth K N Avatar asked Apr 07 '09 03:04

Navaneeth K N


People also ask

What is the use of Makefile in a project?

The project usually have different files to compile and to make the executable binaries. All these different files have dependencies. For example, one source file must be compiled before other one and likewise… Basically, the makefile is used to order the sequence of a compilation of files.

How to include all the source files in the Makefile?

Include all the source files in the makefile. Set the rule and dependencies according to your project needs. Simply run make command. Damn it! This will help you to avoid your big and gusty way of writing compiler commands.

How to quickly create files inside nested directories using terminal-advancednewfile?

This brief guide explains how to quickly create files inside nested directories using "terminal-AdvancedNewFile" tool in Linux and Unix-like operating systems. 1. Create multiple directories and files 1.1. Create multiple directories using mkdir command 1.2. Create multiple files using touch command 2. Install terminal-AdvancedNewFile in Linux 3.

How do I set a variable in the Makefile?

If you want to set the variable in the makefile even though it was set with a command argument, you can use an override directive, which is a line that looks follows− The make program is an intelligent utility and works based on the changes you do in your source files.


2 Answers

There are more modern build systems like SCons That have an easier syntax than make and avoid many pitfalls of make. For example SCons scans the source files to determine dependencies on it's own, whereas for make one needs to specify dependencies manually. If you e.g. add a new #include statement to an implementation file make will not recompile that implementation file if the header changes (unless you add the new dependency). SCons will automatically detect the new dependency and recompile whatever necessary.

like image 93
lothar Avatar answered Oct 05 '22 01:10

lothar


There is a lot to be said for not doing this. Read Recursive Make Considered Harmful. Also in PDF form.

The short short version is that recursive make effectively builds several disjoint but possibly overlapping dependency trees and can not guarantee either a correct or maximally efficient build. The problem get worse if you hope to build in parallel.

To solve the problem you arrange an single, non-recursive make that builds a single all-spanning dependency tree which allows make to solve all the above problems.

Example structures for non-recursive make and solutions to the several tricky problems that come up in writing them can be found in the original paper and in the answers to:

  • What is your experience with non-recursive make?
  • Recursive Make - friend or foe?
like image 26
dmckee --- ex-moderator kitten Avatar answered Oct 04 '22 23:10

dmckee --- ex-moderator kitten