Recursing macro preprocessing?

Discussion in 'C++' started by Henrik Goldman, Oct 21, 2006.

  1. I have an application which compiles on a number of different platforms. Now
    I'm trying to incorporate an external library which is only available to a
    subset of the platforms that my application support.
    This is ok as it will give increased usability on some platforms but not
    all.

    The problem however is to remove code at compiletime when my application is
    compiled on a platform which does not support this library.

    Using the preprocessor I can enumerate all the platforms on which this
    library support like:

    #if (defined(_WIN32) && defined(_M_IX86)) || \

    (defined(_WIN64) && defined(_M_AMD64)) || \

    (defined(__APPLE__) && defined(__ppc__)) || \

    (defined(__APPLE__) && defined(__i386__)) || \

    ....

    #endif

    However in those places of my application where the support code to use the
    library is defined I'd like to condtionally enable the code on a platform
    where support is present.

    More specifically I'd like to do:



    INCLUDE_MY_LIB_SUPPORT_START

    printf("This platform has library support\n");

    INCLUDE_MY_LIB_SUPPORT_END

    On platforms with library support it the macro would expand to nothing. On
    unsupported platforms it would expand to #if 0 and #endif and thus removed
    by the compiler.

    The first few tests showed however that this is not easily possible. The
    alternative is then something like:

    #ifdef INCLUDE_MY_LIB_SUPPORT

    printf("This platform has library support\n");

    #endif

    This is not as good however as one can easily forget to include the needed
    header file and then you get no warning but code on supported platforms will
    still be removed.

    Is there a way to solve this? One other thing is to define it at compile
    time through a switch like -DMY_LIB_SUPPORT but I'd like to keep down the
    number of switches on commandline.

    Thanks.

    -- Henrik
     
    Henrik Goldman, Oct 21, 2006
    #1
    1. Advertising

  2. Henrik Goldman

    Kaz Kylheku Guest

    Henrik Goldman wrote:
    > On platforms with library support it the macro would expand to nothing. On
    > unsupported platforms it would expand to #if 0 and #endif and thus removed
    > by the compiler.


    Not possible. It is not possible to produce a preprocessing directive
    using preprocessing. If you produce the tokens ``#if 0'', they will not
    be treated as a preprocessing directive, even though they look like
    one.

    > The first few tests showed however that this is not easily possible. The
    > alternative is then something like:
    >
    > #ifdef INCLUDE_MY_LIB_SUPPORT
    >
    > printf("This platform has library support\n");
    >
    > #endif
    >
    > This is not as good however as one can easily forget to include the needed
    > header file and then you get no warning but code on supported platforms will
    > still be removed.


    Use integer values, instead of testing for symbol existence:

    #if INCLUDE_MY_LIB_SUPPORT
    #endif

    Now you either have

    #define INCLUDE_MY_SUPPORT 0

    or

    #define INCLUDE_MY_SUPPORT 1

    If you forget to define it, then the #if is ill-formed, having no
    expression.
     
    Kaz Kylheku, Oct 21, 2006
    #2
    1. Advertising

  3. Henrik Goldman

    Greg Comeau Guest

    In article <>,
    Kaz Kylheku <> wrote:
    > #if INCLUDE_MY_LIB_SUPPORT
    > #endif
    >
    >Now you either have
    >
    > #define INCLUDE_MY_SUPPORT 0
    >
    >or
    >
    > #define INCLUDE_MY_SUPPORT 1
    >
    >If you forget to define it, then the #if is ill-formed, having no
    >expression.


    Fairly certain in the case above it is well formed as any ids
    not replaced become replaced by a 0 token.
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 22, 2006
    #4
  4. Henrik Goldman

    Kaz Kylheku Guest

    Greg Comeau wrote:
    > In article <>,
    > Kaz Kylheku <> wrote:
    > > #if INCLUDE_MY_LIB_SUPPORT
    > > #endif

    [ snip ]
    > >If you forget to define it, then the #if is ill-formed, having no
    > >expression.

    >
    > Fairly certain in the case above it is well formed as any ids
    > not replaced become replaced by a 0 token.


    Ah crap, right.

    Then, to defeat all this defaulting, you can to impose an enumeration.
    For instance, the rule that two symbols must be defined:

    #if defined INCLUDE_MY_SUPPORT
    ...
    #elif !defined NO_INCLUDE_MY_SUPPORT
    #error must explicitly request no support
    #endif

    The obvious alternative way to solve the original problem is to create
    an API which is always defined, and always called. On the subset of the
    platforms that have the special library, the API calls the library. On
    the platforms where it doesn't, the API is just stubs that do the right
    kind of nothing for each function.

    // In the header file

    #if .. test for all the platforms where library is supported ...
    // #include or otherwise define the library API
    int library_function();
    #else
    // define dummy version of the API which does nothing, using inline
    functions
    inline int library_function() { return 0; }
    // or, using macros:
    #define library_function() ((int) 0)
    #endif

    And so now there is no special dance to repeat in the sources, where
    you simply call the function:

    library_function(); // might do nothing if not supported

    This also takes care of the forgotten header include.
     
    Kaz Kylheku, Oct 22, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. snowdy

    Recursing code problem

    snowdy, Aug 29, 2003, in forum: C Programming
    Replies:
    19
    Views:
    1,204
    Kevin D. Quitt
    Sep 2, 2003
  2. Scott Carlson

    recursing through files in a folder

    Scott Carlson, Oct 1, 2004, in forum: Python
    Replies:
    3
    Views:
    378
    Mirko Zeibig
    Oct 1, 2004
  3. Francois Grieu
    Replies:
    6
    Views:
    396
    Ben Bacarisse
    Jan 18, 2008
  4. dhun

    Macro preprocessing

    dhun, Nov 7, 2009, in forum: C Programming
    Replies:
    1
    Views:
    359
    Andrey Vul
    Nov 7, 2009
  5. entik

    macro preprocessing C51 x armcc

    entik, Mar 23, 2012, in forum: C Programming
    Replies:
    1
    Views:
    402
    entik
    Mar 23, 2012
Loading...

Share This Page