common #define through all header files and libraries
Sorry for stupid question, I am a newbie in makefiles and libraries. I have a bunch of implementation *.cc files in source folder and a bunch of header files *.h and libraries *.templates.h in include folder. In the makefile I have
Code:
cc-files = $(shell echo source/*.cc)
o-files = $(cc-files:source/%.cc=lib/$(prm)d/%.$(OBJEXT))
go-files = $(cc-files:source/%.cc=lib/$(prm)d/%.g.$(OBJEXT))
h-files = $(wildcard include/*.h)
# While the ChangeMe is remade every time it is missing...
ChangeMe: include/DoNotChangeMe.default
cp include/DoNotChangeMe.default ChangeMe
Makefile: ChangeMe
touch Makefile
I have global options A and B which influence all code files above. If I would have one file I would use
Code:
#define OPTION A
#if OPTION == A
...
#endif
#if OPTION == B
...
#endif
to choose what code I want. Here in ChangeMe I also can define OPTION =1 or OPTION=2 and then use if(OPTION==1) {...} in the code. But is it possible to go through #define which would exclude compilation of unnecessary code?
Best,
Serg
Last edited by StudentFS; March 23rd, 2009 at 11:46 AM.
Re: common #define through all header files and libraries
Originally Posted by laserlight
Typically, you can define the macro by providing a command line option.
Can command line options be used in macros? I did not know about such a possibility. Are we talking about arguments of the function main(int argc, ...) or about compiler options?
Last edited by StudentFS; March 23rd, 2009 at 12:02 PM.
Re: common #define through all header files and libraries
Originally Posted by StudentFS
Can command line options be used in macros? I did not know about such a possibility. Are we talking about arguments of the function main(int argc, ...) or about compiler options?
Re: common #define through all header files and libraries
Originally Posted by StudentFS
Problem is that option A and B are very similar, only minor modifications but in multiple places. To keep two copies of the code is too big headache.
Oh, absolutely you should avoid code duplication. I just feel that using #define should be restricted to OS- or compiler-dependent differences. Algorithmic differences almost always can be encapsulated more cleanly using other approaches.
Re: common #define through all header files and libraries
Thank you for answers.
Originally Posted by Lindley
Oh, absolutely you should avoid code duplication. I just feel that using #define should be restricted to OS- or compiler-dependent differences. Algorithmic differences almost always can be encapsulated more cleanly using other approaches.
I recently encountered a situation where that was not possible. It was finite-element library, where they implemented a complex hierarchy of templates but many features were implemented only for 2D case, not for 1D. It is understandable - a 1D finite element does not have such an element, for example, as a face or edge. But I had a long algorithm which was similar for both cases 1D and 2D but did not work because when I compiled it, g++ complained that many elements are not defined for 1D. I mean, something behind if(dimension!=1) does not work because it is compiled also. There were three choices: 1. double the code, 2. redevelop not my library, 3. use #define. The last was 100 times simpler.
Bookmarks