Makefiles - northern-bites/nbites GitHub Wiki

make and Makefiles

The reason why make was built was because calling of the compiler commands to generate code manually can become very tedious very quickly. Also, when dealing with a larger project, there is the problem of which libraries to compile first, since some libraries will depend on others at link time.

Thus make was born. make can parse a Makefile and then decide the orders in which it will call the commands to compile the code. Makefiles structure themselves around the concept of 'targets'. A target is usually the file that is produced as the output of the user-specified compilation command. Targets don't always have to be real files, as sometimes they can be just name labels (or virtual targets) (NBites e.g. : when you make cross, there is no actual cross file being generated). The user also has to manually specify the files or the other targets that each target depends on. For example, object files will depend on source files for successful compilation, but you can have virtual targets depend on virtual targets and so forth.

You can read more about make and Makefile in this tutorial. It was the first that popped up on google search, and on quick overview it looked comprehensive enough, but there are many a good tutorial out there in the world of the web.

Makefiles in the src folder context

We use manually coded makefiles in order to make the cmake calls that will generate the makefiles needed to build our code. Therefore these makefiles are usually made up of mostly virtual targets, and none of them actually make any calls to gcc. After cmake is done doing its thing, then these makefiles will also call make on the CMake generated Makefiles. The cool thing is that any flags passed to the original make command get passed down to the new make command (NBites e.g. if you call make -j4, then it's as if you are calling make -j4 on the CMake generated Makefiles as well, which is what actually speeds up the compiling of the code!). You can look at the QTool Makefile for a good example of this.

Makefiles in the build folder context

Any makefiles that you might find in the build folder are automatically generated by CMake! These are the Makefiles that are actually used to build the final binary! For example, if you go in build/man/cross (after first having CMake generate the Makefiles, this is what make cross do right now) and do make vision then make will build the vision library (and its dependencies)!.