Makefile

This is a discussion on Makefile within the Linux Programming forums, part of the Platform Specific Boards category; Hello.
Is it possible somehow to specify a debug option in a Makefile. So I can run
> make debug ...

Are you talking about #ifdef'ing stuff out? If that is the case, you need something like this:

make EXTRA_CFLAGS=-DMYDBGVAR=2234 modules

--- since you appear to be using the kernel source build, this will trigger a "special" flag that tells the make system to build with the extra compiler flags -DMY<bla> -- which is like a #define. So, you can then ifdef out stuff from your module.

You need to do some macro programming in your makefile for this. It's been awhile, so I don't remember the syntax, but it is pretty common to set up your makefile to allow something like:make RELEASE=1 target

One problem you WILL encounter is that the makefile is completely unaware of changes in flags. So if you do a "make BUILD=release" followed by a "make BUILD=debug" it will NOT rebuild anything, since as far as make knows, nothing has changed. You need to "make clean" before switching to a different build type. It is easy to forget this and end up with corrupted or incomplete builds.

It's possible to write some complicated make stuff to hack around this problem, but it's not very robust. In general, make sucks for this sort of thing (make is a very primitive build system, period)

I am lazy and use CMake

CMake is a nice cross-platform make system (have used it on projects spanning linux, windows and mac). The only reason that I mention this is that CMake makes makefiles simpler and setting special build settings like DEBUG, OTHER PLATFORM or whatever almost childs-play....

A trick to make sure clean is done for a different kind of a build is... Name your build log file according to the build type, check the logfile name each time you build, and clean if needed (if name differs from current option).

A trick to make sure clean is done for a different kind of a build is... Name your build log file according to the build type, check the logfile name each time you build, and clean if needed (if name differs from current option).

It works, but forcing a clean is really painful. Just because you are flipping between debug and release (for instance, tracking down bugs, then profiling, iteratively) shouldn't require you to rebuild the entire project every time. The problem is that most simple makefile systems place the generated targets in the same directory as the source code, making it impossible to keep multiple builds around simultaneously.

Again, it's POSSIBLE to do all this with make, but it feels like forcing the tool to do something it's not really meant to do. I long ago switched to Boost.Build and never looked back

Use separate build output directories

I think the best option to solve the overhead of switching between release/debug build etc is to take a leaf out of the Linux kernel build system. Write the Makefiles in such a way that all .o, .a, .so or whatever output files which get created are put in different directories. A general convention is to use a directory tree similar to the source tree, and change the parent directory only.