Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Get expanded macros from precompiler and insert as comments in source code?

Get expanded macros from precompiler and insert as comments in source code?

Scheduled Pinned Locked Moved C / C++ / MFC
hardwaretoolstutorialquestion
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    I am working with embedded C and sometimes I compile with Eclipse/GCC and sometimes with STMicroelectronics System Workbench. In my code there are a lot of nested macros and I think it would be great to display the expanded macro value as a comment right after the macro (note: only the macros without parameters). So I was thinking of having a post-build .exe-file/script that takes the result from the precompiler and puts it back into the source code as comments after the macros. As an alternative, I could have a pre-build .exe-file/script that executes the pre-compiler (meaning it will be executed twice for each compile) and then modifies the source code. I feel fairly confident about writing the .exe-file/script, but I have no idea how to execute the precompiler "on demand" or how to view the results of it. Could someone please provide some guidelines on how to achieve this? I know pretty much nothing about makefiles, precompilers, compilers, etc, so please feel free to formulate your answers in this thread in the way you would communicate with a beginner programmer.

    L 1 Reply Last reply
    0
    • A arnold_w

      I am working with embedded C and sometimes I compile with Eclipse/GCC and sometimes with STMicroelectronics System Workbench. In my code there are a lot of nested macros and I think it would be great to display the expanded macro value as a comment right after the macro (note: only the macros without parameters). So I was thinking of having a post-build .exe-file/script that takes the result from the precompiler and puts it back into the source code as comments after the macros. As an alternative, I could have a pre-build .exe-file/script that executes the pre-compiler (meaning it will be executed twice for each compile) and then modifies the source code. I feel fairly confident about writing the .exe-file/script, but I have no idea how to execute the precompiler "on demand" or how to view the results of it. Could someone please provide some guidelines on how to achieve this? I know pretty much nothing about makefiles, precompilers, compilers, etc, so please feel free to formulate your answers in this thread in the way you would communicate with a beginner programmer.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See Overall Options (Using the GNU Compiler Collection (GCC))[^]. The -E option of gcc allows you to stop the build after running the preprocessor. The out put will be the expanded source. But be aware that this will include the expansions of all input from included headers so may well be quite extensive. If you run a few samples you should be able to see how to get to the source that you are interested in.

      A 1 Reply Last reply
      0
      • L Lost User

        See Overall Options (Using the GNU Compiler Collection (GCC))[^]. The -E option of gcc allows you to stop the build after running the preprocessor. The out put will be the expanded source. But be aware that this will include the expansions of all input from included headers so may well be quite extensive. If you run a few samples you should be able to see how to get to the source that you are interested in.

        A Offline
        A Offline
        arnold_w
        wrote on last edited by
        #3

        I just tried it, but it seems I need to reference the macros in order to get the expanded values. Most of my macros are never referenced in the project I would like to generate the explanatory comments in, they are just defined in a (shared) .h-file.

        L 1 Reply Last reply
        0
        • A arnold_w

          I just tried it, but it seems I need to reference the macros in order to get the expanded values. Most of my macros are never referenced in the project I would like to generate the explanatory comments in, they are just defined in a (shared) .h-file.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          If they are not used then the preprocessor will ignore them. Macro expansion happens at the reference, not at the definition. You may need to create some dummy file to get the expansions. A better idea would be to write proper documentation comments so they can be seen by anyone looking at the definitions. And I think that may also make them visible through intellisense.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups