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).

7 Responses to Using multiple FlasCC SWCs in a Flash project

  1. Boder says:

    and How can i define two functions in one swc?

    • stshongr says:

      @Boder – If you look at the code above it has a function defined with some annotations:

      void decrementNumber() __attribute__((used,
      annotate(“as3sig:public function decrementNumber(n:Number):uint”),
      annotate(“as3package:MyLibrary”)));

      void decrementNumber(){

      }

      The annotations describe what the ActionScript interface will be. You could have many different functions in a SWC, for example:

      // First function
      void myFunction1() __attribute__((used,
      annotate(“as3sig:public function myFunction1(n:Number):uint”),
      annotate(“as3package:MyLibrary”)));

      void myFunction1(){

      }

      // Second function
      void myFunction2() __attribute__((used,
      annotate(“as3sig:public function myFunction2(n:Number):uint”),
      annotate(“as3package:MyLibrary”)));

      void myFunction2(){

      }

      // Third function
      // …

      Check out the samples included with the FlasCC build to learn more about generating SWCs. Sample 05_SWC demonstrates how you can make a SWC out of the MurmurHash library which includes many functions in a single SWC.

      If you find it tedious to write these annotations by hand for each function you should check out sample 06_SWIG which demonstrates how you can use SWIG to automatically generate these annotations.

    • Boder says:

      Thank you very much!

  2. tPS says:

    Hello,

    when i try to compile i the two swc files i get the following Error:

    java.lang.NullPointerException
    at flascc.AlcTool.swfresolve(AlcTool.java:468)
    at flascc.AlcTool.main(AlcTool.java:55)

    i am using OSX Lion

  3. Dasa says:

    Is it possible to have one FlasCC SWC that depends an another FlasCC SWC?

    I’d like to create A.swc that some apps may use and B.swc that other apps can use where B.swc has a dependency on A.swc and the developer would need to include both SWCs in their path.