Thread: Makefile

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    19

    Makefile

    Hello.

    Is it possible somehow to specify a debug option in a Makefile. So I can run

    > make debug module

    The only solution I have found now is to use debug-module for every module I have.

    Anyone knows?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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.

    Clear as mud?

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    19
    Well it's really not the precompiler-stuff I'm asking for. I have that worked out.
    I just wonder if I can do something like:
    Code:
    debug: target
                            gcc -Wall ...
    if the Makefile so I can execute the Makefile like
    > make debug target
    or
    > make target -debug
    or something like that...

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    One thing that you could do is
    Code:
    #ifdef DEBUG
    #include "source-dbg.h" 
    #else
    #define function_debug(...)
    #define function1_debug(...)
    //etc
    #endif
    Next, make your make file with two different rules, one that links ONLY the source file, the other that links the source file with the source-dbg.o.

    Again, have I confused you enough?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    19
    Yes, bithub, that's what I need. Do you know where I can get more information about this? Or does anyone else know how to do it?

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    --------------------------------
    
    # File "Makefile"
    
    BUILD = release # Default is release
    -include $(BUILD).mak
    
    # Rest of your makefile
    
    --------------------------------
    
    # File "release.mak"
    
    CFLAGS = whatever cflags you want to use for release
    
    --------------------------------
    
    # File "debug.mak"
    
    CFLAGS = whatever cflags you want to use for debug
    
    --------------------------------
    
    # File "supergoofy.mak"
    
    CFLAGS = some super goofy flags
    Then, when you build:

    make target # defaults to release
    make target BUILD=release # explicitly specify release build
    make target BUILD=debug # explicitly specify debug build
    make target BUILD=supergoofy # explicitly specify the super goofy build

    Etc...

    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)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875

    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....

    Check it out at CMake - Cross Platform Make.

    My 0.02

  9. #9
    C/Linux Programmer
    Join Date
    Dec 2009
    Posts
    17

    Alternate for smaller makefiles

    To add to brewbuck's inputs, an alternative would be something like this (if you need just a small single Makefile):
    Code:
    BUILD ?= release # default build option
    CFLAGS = flags common to all builds
    
    ifeq("$(BUILD)","release")
        CFLAGS += whatever cflags you want to use for release
    endif
    ifeq("$(BUILD)","debug")
        CFLAGS += whatever cflags you want to use for debug
    endif
    ifeq("$(BUILD)","supergoofy")
        CFLAGS += some super goofy flags
    endif
    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).

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by technam View Post
    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
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    there is also an alternative with recent versions of (gnu?)make by updating the flags according to the current rule:

    CFLAGS=<flags>

    debug: CFLAGS+=<debug flags>
    debug: all

    all: ...

    then $ make debug
    it doesn't solve the issues raised in the last post though.

  12. #12
    C/Linux Programmer
    Join Date
    Dec 2009
    Posts
    17

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile help please
    By TriKri in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2009, 12:36 AM
  2. Replies: 2
    Last Post: 08-11-2009, 06:45 AM
  3. Building a project using a Makefile
    By starcatcher in forum Windows Programming
    Replies: 2
    Last Post: 11-23-2008, 11:50 PM
  4. makefile blues....
    By WaterNut in forum C Programming
    Replies: 6
    Last Post: 05-30-2005, 08:22 PM
  5. Need help with Makefile
    By xshapirox in forum C++ Programming
    Replies: 14
    Last Post: 09-28-2004, 03:32 PM