Conditional Makefile

Discussion in 'Programmer's Corner' started by sakitten123, Feb 9, 2016.

  1. sakitten123

    Thread Starter New Member

    Dec 9, 2015
    I have an embedded project that needs to run on two different flash memory sizes of the microcontroller family. All code is written in C.

    The code is exactly the same except:
    - a few #defines in a header file
    - a few variable values in a function in a source file

    I feels unnecessary to have two separate branches in source control, two places to do bug fixes, two FW versions to test and validate before product release etc. Especially since the differences are so small..?

    What I would like is to have two versions of the header file in question and two versions of the source file in question. And then have some condition in the Makefile to compile one of the two. Sounds reasonable?

    Something like this:
    Code (Text):
    2. Makefile
    3. ifeq (condition)
    4. C_OBJECTS += SourceFile1.o
    5. endif
    6. ifeq (condition)
    7. C_OBJECTS += SourceFile2.o
    8. endif
    And SourceFile1 and 2 respectively include header file 1 and 2 as:
    Code (Text):
    2. SourceFile1.c
    3. #include "HeaderFile1.h"
    4. //code
    Code (Text):
    2. SourceFile2.c
    3. #include "HeaderFile2.h"
    4. //code
    The problem is that HeaderFile1.h is included in quite a lot of the other source files in the project. I guess their includes would have to change to HeaderFile2.h based on the Makefile condition. That seems a bit harder to accomplish. With my above reasoning it would mean having to have two different versions of all source files that includes that paricular header file and compile one of the two based on the condition. That's not what I'm after.

    Do I want to do something that is not possible? Do you have any other ideas of how to solve this?

    Any help would be greatly appreciated!
  2. MrChips


    Oct 2, 2009
    Use conditional compilation.
    Use one makefile, one header file and one set of source files.

    In your header file, define something like:

    In your source code, use conditional compilation:


    // code for large memory here


    // code for small memory here