I'm writing a program in c and I want to optionally compile a translationunit based on whether the user wants to or not. Basically I have two files, one main.c which should always be compiled. Moreover I have a file let's, call it optional.c. Some functions in optional.c are much faster than those in main.c but they are also more prone to errors.
In my main.c I can write code like
#ifdef USE_OPTIONAL
some function call from optional.c
#else
some function call from main.c
#endif
In my makefile I also want to compile optional.c only if it should be used, say
gcc -c optional.c
gcc -c -DUSE_OPTIONAL main.c
gcc main.o optional.o
and if not, simply
gcc -c main.c
gcc main.o
It works to have two different rules, one called "all:" and one called "optional:" but I would prefer only typing "make" instead of "make optional". What is the simplest way to implement this? Should I turn to a configure script and a Makefile.in or is it better to just have a second rule? Is it possible to implement for example "make USE_OPTIONAL=1" and then have make read the input of the variable USE_OPTIONAL to figure out what to do?