Using multiple FlasCC SWCs in a Flash project

FlasCC allows you to compile C and C++ libraries into SWCs that can be dropped into any Flash project. This post demonstrates how you can include multiple FlasCC SWCs in an ActionScript project.

To demonstrate this we are going to start with two very simple C libraries that each have one function that either increments or decrements a number. Discussing the source code of these C files is outside the scope of this blog post, but if you aren’t sure how it works you can learn more about the annotate, inline_as3, and AS3_Return functions by looking at the samples that ship with the FlasCC SDK. Specifically look at /samples/02_Interop and /samples/05_SWC. These samples are also discussed in more detail in the documentation that ships with the FlasCC SDK at docs/samples.html.

Here is the source code for the first library MyLibrary.c:

#include <stdlib.h>
#include "AS3/AS3.h"
 
void incrementNumber() __attribute__((used,
	annotate("as3sig:public function incrementNumber(n:Number):uint"),
	annotate("as3package:MyLibrary")));
 
void incrementNumber()
{
    int numberToIncrement = 0;
    inline_as3("%0 = n;\n" : "=r"(numberToIncrement));
    numberToIncrement++;
 
    AS3_Return(numberToIncrement);
}
 
int main()
{
    // The SWC still needs a main() function.  
    // See the code comments in samples/05_SWC for more details.
    AS3_GoAsync();
}

The second library is very similar except it has a decrementNumber() function. Here is the source code for MyLibrary2.c:

#include <stdlib.h>
#include "AS3/AS3.h"
 
void decrementNumber() __attribute__((used,
	annotate("as3sig:public function decrementNumber(n:Number):uint"),
	annotate("as3package:MyLibrary2")));
 
void decrementNumber()
{
    int numberToDecrement = 0;
    // bring the AS3 value into C
    inline_as3("%0 = n;\n" : "=r"(numberToDecrement));
    numberToDecrement--;
 
    AS3_Return(numberToDecrement);
}
 
int main()
{
    // The SWC still needs a main() function.  
    // See the code comments in samples/05_SWC for more details.
    AS3_GoAsync();
}

The first step is to compile these C files into SWCs using gcc.

~/flascc/sdk/usr/bin/gcc MyLibrary.c -emit-swc=MyLibrary -o MyLibrary.swc
~/flascc/sdk/usr/bin/gcc MyLibrary2.c -emit-swc=MyLibrary2 -o MyLibrary2.swc

Now lets create a simple ActionScript application that will consume both of these SWCs.

This simple ActionScript application will just call each function in each SWC. Here is the ActionScript source code for demo.as:

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.Event;
    import MyLibrary.CModule;
    import MyLibrary2.CModule;
 
    public class demo extends Sprite {
        public function demo() {
            addEventListener(Event.ADDED_TO_STAGE, initCode);
        }
 
        public function initCode(e:Event):void {
            // create a TextField to show the output
            var tf:TextField = new TextField();
            addChild(tf);
 
            // Start the FlasCC libraries
            MyLibrary.CModule.startAsync(this);
            MyLibrary2.CModule.startAsync(this);
 
            var x:int = 4;
            var xpp:int = MyLibrary.incrementNumber(x);
            var xmm:int = MyLibrary2.decrementNumber(x);
 
            // show the output
            var s:String;
            s  = "x++ = " + xpp + "\n";
            s += "x-- = " + xmm + "\n";
            tf.appendText(s);
            trace(s);
        }
    }
}

Notice that the ActionScript application must import the CModule and call startAsync() on each SWC.

Now lets compile this ActionScript file into a SWF:

/path/to/flex/bin/mxmlc -static-link-runtime-shared-libraries -library-path=MyLibrary.swc,MyLibrary2.swc demo.as -o demo.swf

If you then launch the resulting SWF you should see the following output:

x++ = 5
x-- = 3

Instead of calling mxmlc directly on the command line you could have also created an ActionScript project in Flash Builder and then added the two SWCs to your library path (Project > Properties > ActionScript Build Path > Add SWC).