This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

is this possible

It happened again.

I have many cases of modules with something like
#ifdef TYPEA
... many lines
#endif
#ifdef TYPEB
... many similar lines
#endif
#ifdef TYPEC
... many similar lines
#endif

It happens that I am looking at the type b code trying to figure out something in type a (the codes are VERY similar).

To avoid this, I have tried

blah,blah // comment TYPEA

and that "kind of works"

Now, the ideal would be if for type a I could do the following:
#ifdef TYPEA
#define ATYPE
#define BTYPE //
#define CTYPE //
#endif

and then just make it
ATYPE blah,blah // comment

Of course this does not work.
Does anyone have a trick that makes this possible?

Erik

Parents
  • "This would be a lot easier by putting these segments into separate files, and then in the main file just writing ..."

    And for #include, the preprocessing tokens after include in the directive are processed just as in normal text (i.e., subject to macro replacement), so one can locate all the names in the source file's front matter or separate header file, leaving the code in the function's body relatively clean and free of #if/#elif/#else/#endif if that's desireable.

    #if defined(VARIANT_A)
    # define FRAGMENT_FOO1  "fragment_foo1_A.c"
    # define FRAGMENT_FOO2  "fragment_foo2_A.c"
    # define FRAGMENT_FOO3  "fragment_foo3_A.c"
    #elif defined(VARIANT_B)
    # define FRAGMENT_FOO1  "fragment_foo1_B.c"
    # define FRAGMENT_FOO2  "fragment_foo2_B.c"
    # define FRAGMENT_FOO3  "fragment_foo3_B.c"
    #elif defined(VARIANT_C)
    # define FRAGMENT_FOO1  "fragment_foo1_C.c"
    # define FRAGMENT_FOO2  "fragment_foo2_C.c"
    # define FRAGMENT_FOO3  "fragment_foo3_C.c"
    #endif
    
    void foo(void)
    {
        #include FRAGMENT_FOO1
        /*
         * ... some code ...
         */
        #include FRAGMENT_FOO2
        /*
         * ... some more code ...
         */
        #include FRAGMENT_FOO3
    }
    

Reply
  • "This would be a lot easier by putting these segments into separate files, and then in the main file just writing ..."

    And for #include, the preprocessing tokens after include in the directive are processed just as in normal text (i.e., subject to macro replacement), so one can locate all the names in the source file's front matter or separate header file, leaving the code in the function's body relatively clean and free of #if/#elif/#else/#endif if that's desireable.

    #if defined(VARIANT_A)
    # define FRAGMENT_FOO1  "fragment_foo1_A.c"
    # define FRAGMENT_FOO2  "fragment_foo2_A.c"
    # define FRAGMENT_FOO3  "fragment_foo3_A.c"
    #elif defined(VARIANT_B)
    # define FRAGMENT_FOO1  "fragment_foo1_B.c"
    # define FRAGMENT_FOO2  "fragment_foo2_B.c"
    # define FRAGMENT_FOO3  "fragment_foo3_B.c"
    #elif defined(VARIANT_C)
    # define FRAGMENT_FOO1  "fragment_foo1_C.c"
    # define FRAGMENT_FOO2  "fragment_foo2_C.c"
    # define FRAGMENT_FOO3  "fragment_foo3_C.c"
    #endif
    
    void foo(void)
    {
        #include FRAGMENT_FOO1
        /*
         * ... some code ...
         */
        #include FRAGMENT_FOO2
        /*
         * ... some more code ...
         */
        #include FRAGMENT_FOO3
    }
    

Children
No data