-
March 23rd, 2009, 11:39 AM
#1
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.
-
March 23rd, 2009, 11:43 AM
#2
Re: common #define through all header files and libraries
Typically, you can define the macro by providing a command line option.
-
March 23rd, 2009, 11:44 AM
#3
Re: common #define through all header files and libraries
Often such designs are better served by using inheritance to select the proper object (algorithm) at startup time.
-
March 23rd, 2009, 11:50 AM
#4
Re: common #define through all header files and libraries
The answer is probably to make a common header file with this #define which is to be included in all other libraries with guards, is it?
-
March 23rd, 2009, 11:53 AM
#5
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.
-
March 23rd, 2009, 11:56 AM
#6
Re: common #define through all header files and libraries
Originally Posted by Lindley
Often such designs are better served by using inheritance to select the proper object (algorithm) at startup time.
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.
-
March 23rd, 2009, 12:18 PM
#7
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?
He meant a compiler command line option.
-
March 23rd, 2009, 12:29 PM
#8
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.
-
March 23rd, 2009, 01:00 PM
#9
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.
-
March 23rd, 2009, 01:18 PM
#10
Re: common #define through all header files and libraries
It's always a trade-off between immediate solutions and long-term maintainability. Which option is best depends on the circumstance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|