Create a build environment for "C" project to dynamically select folders during compile time

140 Views Asked by At

Lets say my folder structure is something like this ..

+-- Application
|
+-- MICRO_CONTROLLER_1
|
+-- MICRO_CONTROLLER_2
|
+-- MICRO_CONTROLLER_3

and i have a compile switch ( SELECT_MICRO) set to #define SELECT_MICRO == MICRO_CONTROLLER_1 , then my project should build application with driver files in MICRO_CONTROLLER_1 , similarly if #define SELECT_MICRO == MICRO_CONTROLLER_2 , then application should build application with driver files in MICRO_CONTROLLER_2

Please let me know if there template to achieve the above.

3

There are 3 best solutions below

0
On

This can only work if the directories have only include files. #define is a preprocessor directive. If the directories have source files, you need to solve it at the build system layer, not the preprocessor layer.

Assuming it's just include files, you'd just #include SELECT_MICRO # "Interface.h"

0
On

You can export that particular path of the folder you want to build and supply the path to the executable. You can get further info. in this thread.

How I could add dir to $PATH in Makefile?

Or simply maintain different Makefiles to make different builds and use make -f to run that particular makefile.

I hope this is what you finally want to perform.

0
On

Typically you would define your pre-processor definitions to tell the pre-processor to include only, for instance, MICRO_CONTROLLER_1 blocks of code and ignore everything else.

Something like the following should suffice:

#if defined(MICRO_CONTROLLER_1)
// Block of code that is only available to MICRO_CONTROLLER_1
#elif defined(MICRO_CONTROLLER_2)
// ...
// All other microcontrollers you are supporting would follow this structure.
#endif

Then you would need to define MICRO_CONTROLLER_1. If you're using an IDE for development, there is typically a project option for pre-processor directives. This is where you would define MICRO_CONTROLLER_1. You could then create different "configurations" - one for each of the microcontrollers you are targeting.