macro definition contains #include directive

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bing

    macro definition contains #include directive

    Hi folks,
    Is there a way to define a macro that may contain #include
    directive in its body. If I just put
    the "#include", it gives error C2162: "expected macro formal
    parameter" since here I am not using
    # to concatenate strings. If I use "\# include", then I receive the
    following two errors:

    error C2017: illegal escape sequence
    error C2121: '#' : invalid character : possibly the result of a macro
    expansion

    Any help?

    Thanks,
    Bing Jian

  • Pawel Dziepak

    #2
    Re: macro definition contains #include directive

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Bing wrote:
    Is there a way to define a macro that may contain #include
    directive in its body. If I just put
    <snip>

    No, preprocessor directives in C++ (and C) are not reflective.

    Pawel Dziepak

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

    iEYEARECAAYFAkk R/5QACgkQPFW+cUiI HNrengCgmsl1wT5 R9t4lKh2iu+f2xs PO
    uNMAn0Q4VWBUeVU kLN6UsDB0FzkE3A Y5
    =BCqQ
    -----END PGP SIGNATURE-----

    Comment

    • Noah Roberts

      #3
      Re: macro definition contains #include directive

      Bing wrote:
      Hi folks,
      Is there a way to define a macro that may contain #include
      directive in its body. If I just put
      the "#include", it gives error C2162: "expected macro formal
      parameter" since here I am not using
      # to concatenate strings. If I use "\# include", then I receive the
      following two errors:
      >
      error C2017: illegal escape sequence
      error C2121: '#' : invalid character : possibly the result of a macro
      expansion
      >
      Any help?
      Look at the boost preprocessor metaprogramming library. The
      BOOST_PP_ITERAT E() macro is able to do it. See what it's doing or maybe
      just use it.

      Comment

      • Pawel Dziepak

        #4
        Re: macro definition contains #include directive

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Noah Roberts wrote:
        Look at the boost preprocessor metaprogramming library. The
        BOOST_PP_ITERAT E() macro is able to do it. See what it's doing or maybe
        just use it.
        Macro can be an argument of #include directive and that's how
        BOOST_PP_ITERAT E() is used in boost (it's probably the solution for
        Bing's problem). However, macro can't contain #include directive.

        Pawel Dziepak
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.9 (GNU/Linux)
        Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

        iEYEARECAAYFAkk SEnAACgkQPFW+cU iIHNoI5QCfe8p5B sWgNF5Lc/gkTx+wEkLt
        vuMAmgL1KlnhEUd UfeHDUjmapG7VWo 5D
        =ir8y
        -----END PGP SIGNATURE-----

        Comment

        • Bing

          #5
          Re: macro definition contains #include directive

          On Nov 5, 4:38 pm, Pawel Dziepak <pdzie...@quarn os.orgwrote:
          >
          Noah Roberts wrote:
          Look at the boost preprocessor metaprogramming library. The
          BOOST_PP_ITERAT E() macro is able to do it. See what it's doing or maybe
          just use it.
          >
          Macro can be an argument of #include directive and that's how
          BOOST_PP_ITERAT E() is used in boost (it's probably the solution for
          Bing's problem). However, macro can't contain #include directive.
          >
          Pawel Dziepak
          Thanks for the answers. Just let you know I started a topic on this at
          stackoverflow.c om
          Is there a way to define a macro that contains a #include directive in its body? If I just put the "#include", it gives the error C2162: "expected macro formal parameter" since here I am not usin...

          Comment

          • James Kanze

            #6
            Re: macro definition contains #include directive

            On Nov 5, 9:06 pm, Bing <bing.j...@gmai l.comwrote:
            Is there a way to define a macro that may contain #include
            directive in its body. If I just put the "#include", it
            gives error C2162: "expected macro formal parameter" since
            here I am not using # to concatenate strings. If I use "\#
            include", then I receive the following two errors:
            error C2017: illegal escape sequence
            error C2121: '#' : invalid character : possibly the result of a macro
            expansion
            Any help?
            No. After expansion, "the resulting completely macro-replaced
            preprocessing token sequence is not processed as a preprocessing
            directive even if it resembles one" [§16.3.4/3]. In other
            words, even if there were a way of introducing a sequence
            "#include" in the expansion, it wouldn't be an include
            directive.

            What problem are you trying to solve with this?

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • Pascal J. Bourguignon

              #7
              Re: macro definition contains #include directive

              Bing <bing.jian@gmai l.comwrites:
              Hi folks,
              Is there a way to define a macro that may contain #include
              directive in its body. If I just put
              the "#include", it gives error C2162: "expected macro formal
              parameter" since here I am not using
              # to concatenate strings. If I use "\# include", then I receive the
              following two errors:
              >
              error C2017: illegal escape sequence
              error C2121: '#' : invalid character : possibly the result of a macro
              expansion
              >
              Any help?
              It is not possible with cpp, however it's trivial to do with make and
              sed (or awk or anything else):

              -----------(Makefile)--------------------------

              example.c : example.cm
              sed -e 's/EXPAND(\(.*\))/@#define SOMEVAR 1@#include <\1.h>@#defin e SOMEVAR_\1 2@/g' < example.cm \
              |tr '@' '\012' example.c

              # ...
              -----------------------------------------------


              ----------(example.cm)-------------------------
              /* -*- mode:c -*- */

              EXPAND(ModuleA)

              EXPAND(ModuleB)

              /**** THE END ****/
              -----------------------------------------------



              Then typing make example.c will generate the file:

              -------------(example.c)------------------------------
              /* -*- mode:c -*- */


              #define SOMEVAR 1
              #include <ModuleA.h>
              #define SOMEVAR_ModuleA 2



              #define SOMEVAR 1
              #include <ModuleB.h>
              #define SOMEVAR_ModuleB 2


              /**** THE END ****/
              -------------------------------------------------------

              --
              __Pascal Bourguignon__

              Comment

              Working...