The Linker keyword allows you to add linker flags to your compiled program. This enables linking against static libraries (.a or .lib files) and using external C/C++ code.
Use Linker "flags" to pass flags to the C++ linker:
Linker "-L." ; Add library search path Linker "-lmathlib" ; Link against libmathlib.a
You typically use Linker statements with Extern blocks to declare the external functions.
| Flag | Purpose | Example |
|---|---|---|
| -L<path> | Add library search path | Linker "-L." Linker "-L/usr/local/lib" |
| -l<name> | Link library (libname.a) | Linker "-lmath" Linker "-lmylib" |
| -static | Use static linking | Linker "-static" |
This example uses a custom math library (libmathlib.a):
; Test the Linker keyword with a custom static library
; This test uses the mathlib library
; Tell the linker where to find libraries (current directory)
Linker "-L."
; Link against our mathlib library
Linker "-lmathlib"
; Declare the external C functions from mathlib
Extern
Function MathLib_Add:Int(a:Int, b:Int)
Function MathLib_Multiply:Int(a:Int, b:Int)
Function MathLib_Factorial:Int(n:Int)
EndExtern
Function Main()
Local a:Int = 5
Local b:Int = 3
Local result:Int
Print "Testing MathLib with Linker keyword"
Print "===================================="
; Test addition
result = MathLib_Add(a, b)
Print "Add(5, 3) = " & ToString(result)
; Test multiplication
result = MathLib_Multiply(a, b)
Print "Multiply(5, 3) = " & ToString(result)
; Test factorial
result = MathLib_Factorial(5)
Print "Factorial(5) = " & ToString(result)
Print ""
Print "All MathLib functions tested successfully!"
Return False
EndFunction
Output:
Testing MathLib with Linker keyword ==================================== Add(5, 3) = 8 Multiply(5, 3) = 15 Factorial(5) = 120 All MathLib functions tested successfully!
You can create a static library using g++ or another C++ compiler:
1. Write the C++ code (mathlib.cpp):
// mathlib.cpp
extern "C" {
int MathLib_Add(int a, int b) {
return a + b;
}
int MathLib_Multiply(int a, int b) {
return a * b;
}
int MathLib_Factorial(int n) {
if (n <= 1) return 1;
return n * MathLib_Factorial(n - 1);
}
}
2. Compile to object file:
g++ -c mathlib.cpp -o mathlib.o
3. Create static library:
ar rcs libmathlib.a mathlib.o
4. Use in BambooBasic:
Linker "-L." Linker "-lmathlib" Extern Function MathLib_Add:Int(a:Int, b:Int) Function MathLib_Multiply:Int(a:Int, b:Int) Function MathLib_Factorial:Int(n:Int) EndExtern
You can specify multiple library search paths:
; Search current directory Linker "-L." ; Search libs subdirectory Linker "-Llibs" ; Search absolute path Linker "-L/usr/local/lib" ; Search Windows path Linker "-LC:/MyLibs"
; Add search paths Linker "-L." Linker "-Llibs" ; Link multiple libraries Linker "-lmath" Linker "-lphysics" Linker "-lgraphics" Linker "-laudio" ; Declare functions from all libraries Extern ; Math library Function Math_Sqrt:Double(value:Double) ; Physics library Function Physics_ApplyForce(obj:Int, fx:Double, fy:Double) ; Graphics library Function Graphics_DrawSprite(x:Int, y:Int, sprite:Int) ; Audio library Function Audio_PlaySound(filename:String) EndExtern
When using Linker, you must declare the external functions with Extern:
Linker "-lmylib"
; Declare which functions come from the library
Extern
Function MyLib_Init:Int()
Function MyLib_Process:Int(data:String)
Function MyLib_Cleanup()
EndExtern
Function Main()
; Now you can call them
If MyLib_Init() <> 0
MyLib_Process("Hello")
MyLib_Cleanup()
EndIf
Return False
EndFunction
The Extern block tells BambooBasic:
| Feature | Linker | Import |
|---|---|---|
| Purpose | Link static libraries | Load DLL functions |
| File Type | .a, .lib (static) | .dll, .so (dynamic) |
| Link Time | Compile time | Runtime |
| Declaration | Extern block | .decls file |
| Example | Linker "-lmath" | Import "BBRuntimeLinux.decls" |
Use Linker when:
Use Import when:
Examples of common libraries you might link:
; OpenGL graphics Linker "-lopengl32" Linker "-lglu32" ; SDL2 library Linker "-lSDL2" Linker "-lSDL2main" ; Math library (Unix) Linker "-lm" ; Windows libraries Linker "-luser32" Linker "-lgdi32" Linker "-lkernel32"
Error: "cannot find -lmylib"
Error: "undefined reference to 'FunctionName'"
Use verbose mode to see linker commands:
bamboo_basic.exe -v -s MyProgram.bam -o .
See the Linker Example for a complete demonstration.
BambooBasic © 2026 Michael Denathorn