Skip to content

Flash C Compiler (FlasCC) tips and tricks

Cheng Liao edited this page Jun 23, 2013 · 2 revisions

The Flash C++ Compiler (FlasCC) provides a complete C/C++ development environment based on GCC that you can use to compile C/C++ code to target Adobe Flash Player and Adobe AIR. Using flascc, you can port virtually any of your existing C/C++ code to the web.

This article contains tips and tricks to help you get started with flascc.

Tips for debugging with GDB

Like most GCC-based toolchains, flascc uses GDB as its debugger. It provides a customized version of GDB to debug flascc-compiled C/C++ code that is running within Flash Player.

Setting the FLASCC_GDB_RUNTIME environment variable
To execute GDB, you must define a FLASCC_GDB_RUNTIME environment variable that contains a fully qualified path to either a standalone debugger version of Flash Player or a web browser in which the debugger version of Flash Player is installed. If the path contains spaces use either double quotes (Windows) or escape spaces with a backslash (Mac).

For example:

  • On Windows:
FLASCC_GDB_RUNTIME="/cygdrive/c/Program Files (x86)/Mozilla Firefox/firefox.exe"
  • On Mac OS X:
FLASCC_GDB_RUNTIME= export 
FLASCC_GDB_RUNTIME=/Users/flex/runtimes/player/11.3/mac/Flash\ 
Player\ Debugger.app

Using GDB in Firefox If you are using Firefox you may want to extend or disable the plugin hang detection time to avoid the browser prematurely killing the SWF while you are debugging. For more information on how to do this, see the MozillaZine article on this topic.

Connecting GDB to a SWF hosted on a remote site
If your flascc application is running on a remote site (a staging server, for example), set the FLASCC_GDB_RUNTIME environment variable to point to your browser (as described above) and then pass the URL of your remote application to GDB when you start it, for example:

gdb www.mysite.com/games/mygame.html

Debugging SWCs with GDB
When you compile a SWC, you specify a namespace. So, when you debug that SWC, you need to tell GDB which namespace you want using the set as3namespace command. You can debug only one SWC per session. For more information, see the GDB section of docs/Reference.html of your local flascc installation or online.

Using the GDB call command
While debugging with GDB, you can use the call command to invoke a function in your code. This can be an arbitrary function in which you are interested, or it can be a function that you have added strictly for debugging purposes. For example:

(gdb) call dumpValues()

Rerunning a SWF in GDB (and disregarding the associated warnings)
When you rerun a SWF in GDB, you may see a warning like the following: warning: Temporarily disabling breakpoints for unloaded shared library "remote:0.elf" You can safely ignore these warnings. Here is an example of running and rerunning a SWF in GDB:

<path>/sdk/usr/bin/gdb call.swf
GNU gdb (GDB) 7.3 Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-apple-darwin10 --target=avm2-elf".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) b main
No symbol table is loaded. Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) y Breakpoint 1 (main) pending.
(gdb) r
Starting program: call.swf
0xdddddddd in ?? ()

Breakpoint 1, 0xf0000247 in main (argc=0, argv=0x200ff0) at call.c:66
66 int s = 2;
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
warning: Temporarily disabling breakpoints for unloaded shared library "remote:0.elf"
Starting program: call.swf
0xdddddddd in ?? ()

Breakpoint 1, 0xf0000247 in main (argc=0, argv=0x200ff0) at call.c:66
66 int s = 2;
(gdb) 

Disregarding the "No symbol table is loaded...." message
In certain situations, when you run GDB, you will see the following message:

No symbol table is loaded.... pending on future shared library load

You can safely ignore this warning.

Use monitor eval to view long Strings in GDB GDB does not display ActionScript 3 Strings in the regular list of locals if the String is longer than 80 characters. To view these Strings, use the monitor eval command.

For example, monitor eval myLongVariable displays the String value regardless of the length.

Optimization tips

When you’re done debugging and ready to optimize, consider the following tips.

Generating a fully optimized application
By default, the flascc version of GCC produces ActionScript ByteCode (ABC). This is adequate for most of the development cycle, when fast compile times are what you need. However, for full optimization, use the -emit-llvm or -O4 options when invoking GCC. These options result in LLVM bitcode, which creates a Link Time Optimized (LTO) build. Although it takes longer to compile with these switches, you get a final fully-optimized build with everything compiled as LLVM bitcode. For example, the following command lines create a fully optimized build:

gcc test.c -emit-llvm -c -o test.o
gcc test.c -O4 -c -o test.o
gcc test.c -emit-llvm -emit-swf -o test.swf
gcc test.c -O4 -emit-swf -o test.swf

Using GCC to generate a SWC
When you use GCC to generate a SWC you must use the -emit-swc= option to specify the AS3 package name that will contain the generated code and the internal flascc boilerplate code. This lets you link multiple flascc-generated SWCs into one SWF without any function or class collisions. The following example is from /tutorials/05_SWC/makefile:

"$(FLASCC)/usr/bin/g++" -O4 MurmurHash3.cpp as3api.cpp main.cpp
-emit-swc=sample.MurmurHash -o MurmurHash.swc

For more information, see Building SWCs in docs/Reference.html of your local flascc installation.

Optimizing SWC and SWF size
Although GCC includes many options to minimize output size, the following are the most common techniques:

  • Use the GCC -O4 option to generate LLVM bitcode and create a fully optimized build.
  • Use an exports file to customize what is included in the build. For an example, see /tutorials/12_Stage3D/exports.txt.
  • Experiment with the various optimization options that llvm provides (such as -Os)

Targeting an older SWF
The SWF version determines which version of the AIR and Flash Player APIs are available. For example, SWF 17 equates to AIR 3.4 and Flash Player 11.4; if you specify SWF 16, your application will have access to only the AIR 3.3 and Flash Player 11.3 APIs. If, for some reason, you want to target an older SWF version, use the GCC -swf-version= option (the current default is SWF version 17, so to change it to SWF version 16, you'd specify the following: -swf-version=16
For more information on the mapping between SWF version and Flash Player version, see Creating the extension descriptor file.