Skip to content

Compilation Basics

CacaoLang includes a built-in compiler (cocoac v1.2.5) that can compile your .choco programs into standalone native executables.

Why Compile?

Compiling your CacaoLang programs offers several benefits:

  • Standalone executables - Distribute without requiring the interpreter
  • Performance - Native code execution
  • Deployment - Single file distribution
  • Protection - Source code is embedded and compiled

Basic Compilation

Simple Compilation

Compile a Choco program to an executable:

./choco compile myprogram.choco

This creates an executable named myprogram (or myprogram.exe on Windows) in the current directory.

Custom Output Name

Specify a custom output filename:

./choco compile myprogram.choco -o myapp

This creates myapp (or myapp.exe).

Run the Compiled Program

./myprogram

Compilation Process

When you compile a Choco program, the compiler:

  1. Reads your .choco source file
  2. Embeds the source code into a C++ wrapper
  3. Includes the interpreter and runtime
  4. Compiles everything with g++ to native code
  5. Links required libraries (GTK4 if using GUI)
  6. Outputs a standalone executable

Example

Create hello.choco:

print "Hello from compiled Choco!";

let name = input("What's your name? ");
print "Nice to meet you, #{name}!";

Compile it:

./choco compile hello.choco

Run it:

./hello

Output:

Hello from compiled Choco!
What's your name? Alice
Nice to meet you, Alice!

GUI Applications

Compiling GUI applications works the same way:

// app.choco
gui_init("com.example.app");
let window = gui_window("My App", 400, 300);
let label = gui_label("Hello, GUI!");
gui_add(window, label);
gui_run();

Compile:

./choco compile app.choco -o myguiapp

The compiler automatically detects GTK4 and includes necessary libraries.

Compilation Options

Option Description
-o <name> Specify output filename (without extension)
--no-gui Compile without GUI support (no GTK4 required)
-h, --help Show help message

Compilation Requirements

For Console Programs

  • C++ compiler (g++)
  • C++17 support

For GUI Programs

All of the above, plus:

  • GTK4 development libraries
  • pkg-config

Error Handling

If compilation fails, the compiler will:

  1. Show the error message from g++
  2. Provide troubleshooting suggestions
  3. Leave the temporary files for inspection

Common issues:

  • GTK4 not found: Install GTK4 dev libraries or use --no-gui
  • g++ not found: Install GCC or add to PATH
  • Permission denied: Make sure you have write permissions

Distribution

Once compiled, you can distribute your executable along with:

  • Any data files your program needs
  • GTK4 runtime libraries (for GUI apps)
  • README or documentation

The executable contains all the Choco code and interpreter, so users don't need CacaoLang installed.

Platform Considerations

Linux

Executables are portable across Linux distributions, but users may need GTK4 runtime libraries for GUI apps:

sudo apt install libgtk-4-1

macOS

Distribute .app bundles for better integration. Users need GTK4 runtime via Homebrew.

Windows

Include GTK4 DLLs with your executable or use an installer to set up dependencies.

Next Steps