common #define through all header files and libraries
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: common #define through all header files and libraries

  1. #1
    Join Date
    Dec 2008
    Posts
    54

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,196

    Re: common #define through all header files and libraries

    Typically, you can define the macro by providing a command line option.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    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.

  4. #4
    Join Date
    Dec 2008
    Posts
    54

    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?

  5. #5
    Join Date
    Dec 2008
    Posts
    54

    Re: common #define through all header files and libraries

    Quote Originally Posted by laserlight View Post
    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.

  6. #6
    Join Date
    Dec 2008
    Posts
    54

    Re: common #define through all header files and libraries

    Quote Originally Posted by Lindley View Post
    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.

  7. #7
    Join Date
    Mar 2009
    Posts
    51

    Re: common #define through all header files and libraries

    Quote Originally Posted by StudentFS View Post
    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.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: common #define through all header files and libraries

    Quote Originally Posted by StudentFS View Post
    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.

  9. #9
    Join Date
    Dec 2008
    Posts
    54

    Re: common #define through all header files and libraries

    Thank you for answers.
    Quote Originally Posted by Lindley View Post
    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.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center