Friday, June 5, 2009

Getting Started with Alchemy : Hello World tutorial in Flash CS4 and Flex 3

Today, we are going to do a hello world example for Adobe Alchemy. For those, who don't know what Alchemy is,
Alchemy is a research project that allows users to compile C and C++ code that is targeted to run on the open source ActionScript Virtual Machine (AVM2). The purpose of this preview is to assess the level of community interest in reusing existing C and C++ libraries in Web applications that run on Adobe Flash Player and Adobe AIR.

First up, we have to set up alchemy. You can find the setup instructions in Adobe Labs. InsideRIA also have an article about setting up alchemy. You can find that here.

Now that you have setup Alchemy, lets do the traditional Hello World example with it. First up the C code - helloWorld.c :

#include "AS3.h"

static AS3_Val returnString()
char* text = "Hello World";
return AS3_String(text);

int main()
AS3_Val cMethod = AS3_Function( NULL, returnString );
AS3_Val result = AS3_Object( "returnString : AS3ValType" , cMethod);
AS3_Release( cMethod );
AS3_LibInit( result );
return 0;

Some code explanations:
  • AS3_Val is the data type Alchemy uses to represent ActionScript objects.
  • All functions visible to ActionScript have to declared with AS3_Val as the return type.
  • The first step in main() is to declare all methods for ActionScript as AS3_Function instances.
  • Next, create an AS3_Object which will hold references to all these functions.
  • "Release" the unwanted methods by calling AS3_Release.
  • Finally, notify the runtime that the library has been initialized by using AS3_LibInit. Pass the object containing all the functions visible to ActionScript. Note : This should be called last.
Now that we have written our C code, we have to compile and convert it into a SWC. Open Cygwin, turn on Alchemy using the alc-on; command and navigate to the folder where you have saved your C program. Then type the following command :
gcc helloWorld.c -O3 -Wall -swc -o helloworld.swc
This should create the SWC file with the filename helloworld.swc in that folder.If there were any errors in the C program, it will be shown here.

Now that you have the SWC file, we can use it in Flash or Flex to call its functions.

Using the SWC file in Flash CS4 :

  1. Open Flash CS4 and create a new ActionScript 3 document.
  2. In the Properties panel, click on the Edit profile and click on ActionScript 3 Settings.
  3. In the Library path tab, navigate to the SWC file and add it.
  4. In the Actions panel, type the following code :

    //CLibInit is the Alchemy bridge to the C/C++ methods.
    import cmodule.helloworld.CLibInit;

    var loader:CLibInit = new CLibInit;
    var library:Object = loader.init();
  5. Save and run the file. The text "Hello World" should be displayed in the Output panel.

Using the SWC file in Flex Builder 3 :

  1. Start Flex Builder 3 and create a new ActionScript project.
  2. Right-click the project, select Properies and select ActionScript Build Path. In the Library path tab, add the SWC file.
  3. Add the following code : (My ActionScript project's name is HelloWorld )

    import flash.display.Sprite;

    import cmodule.helloworld.CLibInit;

    public class HelloWorld extends Sprite
    public function HelloWorld()
    var loader:CLibInit = new CLibInit;
    var lib:Object = loader.init();
  4. Save and debug the application. You should see the "Hello World" in the Console.
Congratulations, you have written a function in a C program and called that function in ActionScript using Adobe Alchemy. Hope this post was useful to you. I'll post more in the coming days. Have fun :D


  1. At last! C++!
    My learning curve: Illustrator->Dreamweaver->other Adobe`s products->Javascript->PHP->Flash->Flex->Java->C++
    Hope i wont go to assembler:)

    1. with stage3d- you will! :) (shaders)

  2. Thanks Pradeek, great article! -- can I ask what wordpress plugin you use to display your actionscript code?

  3. nevermind, got it:


  4. Hey, thanks for the article!

    Can you comment a bit further on the include lines? stdio.h

    Im not sure why you have included these lines...

    Also, suppose I have a big lib in c, and all I'm interested from this lib to access from as3 is 1 method and all the other methods in c are auxiliary and private to this nexus method. If I connect this method to as3 the way you do, is that all I would have to do to be able to use the lib from as3?

    Thanks =)

  5. @Li stdio.h is not used in this example and I have removed it. AS3.h contains the C++ API for Alchemy.
    As for your other question, Yes, the method you need use in AS3 has to be declared as shown in the example. Let me know how your project works out. Thanks for the comment.

  6. i have a problem on setting up alchemy, when i run ./config i get a massage Error: "java" not found please give some help

  7. @Anonymous Check out this post.

  8. Thanks Pradeek i will see the forum. What do you do pradeek. I am also from india mumbai.

  9. there is a prob to use the swc package into flex builder, it is not able to find that CLibInit.
    Is there any setting required to use this in flex builder

  10. Hey Pradeek,

    Got it working! And thanks for the InsideRIA link (, definitely the best tutorial on setting up alchemy for windows/cygwin.

  11. Hello Pradeek,
    The InsideRIA link is broken.
    The new link is