# C Preprocessor Fun

**URL:** <https://forum.kirupa.com/t/c-preprocessor-fun/221655>\
**Category:** programming\
**Created:** [April 6, 2007, 12:55am UTC](https://forum.kirupa.com/t/c-preprocessor-fun/221655 "2007-04-06T00:55:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pingnak](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@pingnak](https://forum.kirupa.com/u/pingnak)\
**Post date:** [April 6, 2007, 12:55am UTC](https://forum.kirupa.com/t/c-preprocessor-fun/221655/1 "2007-04-06T00:55:01Z")

</div>

In case you want some nifty ‘hard’ constants, preprocessor-controlled expansion of debug/instrumented/release/code, templates, assertions, to add language elements to AS3, handy-dandy macros of all sorts, and to obfuscate release build output for decompilers, AND you want to develop in a ‘friendly’ environment that will show you the ORIGINAL source code, instead of the CPP output, read on.

**BlankProject.zip**  
The Makefile script is fairly straight-forward, as makefiles go. It has two lower-level scripts (debug.mak and release.mak) that can be invoked with ‘make -f debug.mak’, or that can be invoked from the main Makefile (make debug).

There’s one build rule to make .as3 files into .as files, tidy them up a bit and place them in either the as or asd folder.

There’s one rule to invoke mxmlc if any of the .as files were updated.

There is an extra step to translate the error output so Error and Warning callouts reference the .as3 files in the ‘src’ folder instead of the ‘as’ or ‘as3’ folder, so your friendly IDE can pull up the right file when you have an error.

There is a collection of batch files to invoke make in various ways.

There’s a ‘pp.h’ in the include directory with a few handy preprocessor toys in it. The assert macro should invoke ‘enterDebugger()’, but I can’t seem to find that function in the (free) Flex SDK. What it should be short of that is a call to some readily accessible exported function to stop and wait for user response, or induce a runtime error that brings the debugger up without causing other compile-time or runtime problems.

**The project layout**  
./  
./include  
./src  
./as  
./asd  
./bin[LIST]  
[_] ./include Contains the preprocessor headers. Everything in the ./include folder becomes a dependency for every as file.  
[_] ./as and ./asd contain the preprocessed output for release and debug, respectively.  
[_] ./src Contains the source code. Files in this path end with ‘.as3’ instead of the conventional ‘.as’. This is for two reasons. Foremost, it’s simpler to tell make to translate files that have different extensions. Second, it bugs me that AS1, AS2, AS3 are all completely different languages, yet they all have the same .as extension. Well, not in this project, buddy. Anyway, every ‘as3’ file found there will be preprocessed into as or asd.  
[_] ./bin Contains the .swf output.  
[_] The project root (./) contains some handy batches and such.  
[_] The ./as, ./asd and ./bin folders are all treated as scratch folders. The Makefile will nuke 'em and re-create empty ones whenever you ‘clean’. You should clean once when you initially start up a project.[/LIST]The attached zip file is a ‘Hello World’ app that gets preprocessed and made.

You’ll note (once you’ve made it) that after it’s been build, it builds again VERY fast until mxmlc has to get involved. It very quickly works through the dependencies and doesn’t invoke mxmlc unless it has to.

**Requirements**

First, to play along with the example, you’ll need a few minor things… A passing familiarity with command line tools (especially Unix/Linux ones) is a big plus for understanding this.

GNU Make  
This does the old C ‘make’ script processor. I may be showing my age, but I don’t know ‘Ant’, and I do know ‘Make’. Anyway, it’s the concept that counts.

GNU CPP  
This is the GNU C preprocessor. It inserts text from macros into source code where you drop references to those macros, optionally with parameters.

GNU sed  
The Stream Editor. This tool is so absolutely handy, you won’t be able to live without it… if you ever figure out how to use it. This is needed to do some text translations on the preprocessor and MXMLC output.

Optional: GNU indent  
A ‘Pretty Print’ tool that will make CPP output more readable, when you’re debugging complex preprocessor output. It’s in the Makefile, but commented out, because running it will make the line numbers mismatch on the error call-outs, and you’re not debugging big preprocessor macro-spew very often unless you’re actively developing code generating template macros.

GNU make, sed and indent are already in most major Linux distros, and probably already in later Linux-based Mac machines. The Makefile makes some Windows system calls, so that’ll need to change for Linux/Mac.

Under Windows, you can get the MinGW msysDTK-1.0.1.exe for make, then unzip gcc-core-3.4.2-20040916-1.tar.gz into tht same path for gcc, then grab the balance of the missing tools from ‘GnuWin32’ (and don’t neglect their ‘dependencies’). Basically we assume they’re installed and in the path somehow.  
[http://www.mingw.org/](http://www.mingw.org/)  
[http://gnuwin32.sourceforge.net/packages/](http://gnuwin32.sourceforge.net/packages/)

Under Windows, don’t use the ‘sed’ from the old ‘unxutils’ Windows project. It’s bent.

Cygwin has all of these tools, but you’re on your own for any Windows/Linux path problems that I got when I tried to run Flex under it.

Optional: jEdit  
This is the ‘friendly’ editor that I use. It’s JAVA based, like Eclipse, but the current version comes with an Actionscript (AS2) syntax filter, and it’s very lightweight (compared to Eclipse, anyway).  
[http://www.jedit.org/](http://www.jedit.org/)

Plugins you’ll need: Console, ErrorList  
Plugins you’ll want: BufferTabs, Project Viewer  
Things you’ll need to do:

1. Add an ‘mxmlc’ error pattern to the Console plugin in Plugins-\>Plugin Options-\>Console-\>Error Patterns, click the ‘+’ at the bottom of the Error patterns list.  
[LIST]  
[_]Name: mxmlc  
[_]Error Regexp: (._)([)]: col: (\d+) Error: (._)$  
[_]Warning Regexp: (._)([)]: col: (\d+) Warning: (._)  
[_]Filename: $1  
[_]Line number: $2  
[_]Error message: $4[/LIST]2. Go into Utilities-\>Global Options-\>Editing[LIST]  
[_]Set up your tab preferences  
[_]Set up ‘actionscript’ settings to recognize ‘.as3’ (File name glob: _.{as,as2,as3})[/LIST]3. You’ll probably also want to go to Utilities-\>Global Options-\>Shortcuts and add these keyboard shortcuts, if you’re a Windows user.[LIST]  
[_]Close: Ctrl-F4  
[_]Go to Next Buffer: Ctrl-Tab  
[_]Go to Previous Buffer: Ctrl-Shift-Tab[/LIST]
