Inline C++ - Embedding C++ Code

The InlineCPP keyword allows you to embed raw C++ code directly into your BambooBasic programs. This gives you access to the full power of C++ when needed.



Basic Syntax

Use InlineCPP...EndInlineCPP blocks to embed C++ code:

InlineCPP
// Your C++ code here
void MyFunction() {
    printf("Hello from C++\n");
}
EndInlineCPP

You must declare the function using Extern before calling it from BambooBasic.



Complete Example
; INLINECPP Example

; Firstly, declare the function because in BambooBasic's
; eyes, it is external, and secondly, this adds it to
; auto-complete!
Extern
Function MyFunc()
EndExtern

Function Main()
    MyFunc()
    Return False
EndFunction

; Our raw C++ function within the InlineCPP code block
InlineCPP
void MyFunc()
{
    printf("Hello from inline C++\n");
}
EndInlineCPP

Output:

Hello from inline C++


How It Works
  1. Declare the function using an Extern block
  2. Write C++ code in an InlineCPP block
  3. Call the function from your BambooBasic code


Function with Parameters and Return Value
; Declare the function
Extern
Function AddNumbers:Int(a:Int, b:Int)
EndExtern

Function Main()
    Local result:Int = AddNumbers(10, 20)
    Print "Result: " & ToString(result)

    Return False
EndFunction

; Implement in C++
InlineCPP
int AddNumbers(int a, int b)
{
    return a + b;
}
EndInlineCPP

Output:

Result: 30


Type Mappings
BambooBasic Type C++ Type Example
Int int Function GetValue:Int()
int GetValue()
Double double Function GetPI:Double()
double GetPI()
String const char* Function GetName:String()
const char* GetName()
Void void Function DoWork()
void DoWork()


Working with Strings
Extern
Function GetGreeting:String(name:String)
EndExtern

Function Main()
    Local greeting:String = GetGreeting("Alice")
    Print greeting

    Return False
EndFunction

InlineCPP
#include <string>
#include <cstring>

const char* GetGreeting(const char* name)
{
    // Create greeting string
    static char buffer[256];
    sprintf(buffer, "Hello, %s! Welcome to BambooBasic.", name);
    return buffer;
}
EndInlineCPP

Output:

Hello, Alice! Welcome to BambooBasic.

Important: String return values must point to static or persistent memory, not local stack memory.



Using C++ Standard Library
Extern
Function CalculateDistance:Double(x1:Double, y1:Double, x2:Double, y2:Double)
EndExtern

Function Main()
    Local dist:Double = CalculateDistance(0.0, 0.0, 3.0, 4.0)
    Print "Distance: " & ToString(dist)

    Return False
EndFunction

InlineCPP
#include <cmath>

double CalculateDistance(double x1, double y1, double x2, double y2)
{
    double dx = x2 - x1;
    double dy = y2 - y1;
    return sqrt(dx * dx + dy * dy);
}
EndInlineCPP

Output:

Distance: 5


Multiple Functions in One Block
Extern
Function InitSystem:Int()
Function ProcessData(value:Int)
Function ShutdownSystem()
EndExtern

Function Main()
    If InitSystem() = 0
        ProcessData(42)
        ShutdownSystem()
    EndIf

    Return False
EndFunction

InlineCPP
#include <iostream>

static bool systemInitialized = false;

int InitSystem()
{
    std::cout << "System initialized" << std::endl;
    systemInitialized = true;
    return 0;
}

void ProcessData(int value)
{
    if (systemInitialized) {
        std::cout << "Processing: " << value << std::endl;
    }
}

void ShutdownSystem()
{
    std::cout << "System shutdown" << std::endl;
    systemInitialized = false;
}
EndInlineCPP


Using Windows API
Extern
Function ShowMessageBox:Int(title:String, message:String)
EndExtern

Function Main()
    ShowMessageBox("BambooBasic", "Hello from inline C++!")
    Return False
EndFunction

InlineCPP
#include <windows.h>

int ShowMessageBox(const char* title, const char* message)
{
    return MessageBoxA(NULL, message, title, MB_OK | MB_ICONINFORMATION);
}
EndInlineCPP


Advanced: File I/O
Extern
Function WriteToFile:Int(filename:String, content:String)
Function ReadFromFile:String(filename:String)
EndExtern

Function Main()
    ; Write to file
    If WriteToFile("test.txt", "Hello, World!") = 0
        Print "File written successfully"
    EndIf

    ; Read from file
    Local content:String = ReadFromFile("test.txt")
    Print "File content: " & content

    Return False
EndFunction

InlineCPP
#include <fstream>
#include <string>
#include <cstring>

int WriteToFile(const char* filename, const char* content)
{
    std::ofstream file(filename);
    if (!file.is_open()) return -1;

    file << content;
    file.close();
    return 0;
}

const char* ReadFromFile(const char* filename)
{
    static std::string buffer;
    std::ifstream file(filename);

    if (!file.is_open()) return "";

    buffer.clear();
    std::string line;
    while (std::getline(file, line)) {
        buffer += line;
    }

    file.close();
    return buffer.c_str();
}
EndInlineCPP


Performance Optimization

Use inline C++ for performance-critical code:

Extern
Function FastMatrixMultiply(matrix1:Int, matrix2:Int, result:Int, size:Int)
EndExtern

Function Main()
    ; Create matrices...
    ; Call optimized C++ function
    FastMatrixMultiply(matrixA, matrixB, result, 100)

    Return False
EndFunction

InlineCPP
void FastMatrixMultiply(int* matrix1, int* matrix2, int* result, int size)
{
    // Optimized C++ matrix multiplication
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            result[i * size + j] = 0;
            for (int k = 0; k < size; k++) {
                result[i * size + j] += matrix1[i * size + k] * matrix2[k * size + j];
            }
        }
    }
}
EndInlineCPP


Best Practices

When to Use InlineCPP

Good uses:

Avoid when:



Debugging InlineCPP

Compilation errors in C++ code:

Linker errors:

Runtime crashes:



InlineCPP vs Linker vs Import
Feature InlineCPP Linker Import
Code Location In .bam file Static library DLL file
Use Case Quick C++ snippets Pre-compiled libs Runtime libraries
Declaration Extern block Extern block .decls file
Advantages All in one file
Easy to write
Reusable
Pre-compiled
Dynamic loading
Shared code


Key Points

See Also

Examples

See the InlineCPP Example for a complete demonstration.


BambooBasic © 2026 Michael Denathorn