Getting Started

Basic setup for embedding Behl in your C++ application.

Table of contents

  1. Include and Link
    1. Include Header
    2. Linking
  2. State Management
    1. Creating a State
    2. Loading the Standard Library
    3. Cleaning Up
  3. Complete Example
  4. Next Steps

Include Header

#include <behl/behl.hpp>

All behl functions are in the behl namespace.

Linking

Link against the behl library:

target_link_libraries(your_target PRIVATE behl)

State Management

Creating a State

The behl State is the core runtime object. Create one with new_state():

behl::State* S = behl::new_state();

Returns: Pointer to the new state.

Loading the Standard Library

Load all standard library modules (core, table, gc, debug, math, os, string, fs):

behl::load_stdlib(S);

All modules must be explicitly imported using import().

Cleaning Up

Always close the state when done:

behl::close(S);
S = nullptr;  // Good practice

Complete Example

#include <behl/behl.hpp>
#include <iostream>

int main() {
    // Create state
    behl::State* S = behl::new_state();
    behl::load_stdlib(S);
    
    // Load and run a script
    const char* script = R"(
        let x = 10;
        let y = 20;
        print("Sum: " + tostring(x + y));
        return x + y;
    )";
    
    if (behl::load_string(S, script)) {
        if (behl::call(S, 0, 1)) {
            int result = behl::to_integer(S, -1);
            std::cout << "Result from C++: " << result << "\n";
            behl::pop(S, 1);
        }
    }
    
    // Cleanup
    behl::close(S);
    return 0;
}

Next Steps


Copyright © 2025 behl Project. Distributed under MIT License.

This site uses Just the Docs, a documentation theme for Jekyll.