Skip to content

Latest commit

 

History

History

cpp-functions

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Call C++ functions from Sere

This example calls two C++ functions from Sere through typed extern "C" declarations. It shows the complete source layout, native build setting and executable call for Sere 0.2.2 on Windows x64.

Build and run

Download the official Sere 0.2.2 Windows x64 portable ZIP and extract the complete bundle. Keep its compiler, runtime and standard library together. The release bundles its native toolchain; this example does not require building the compiler or installing a separate C++ toolchain.

The published portable archive SHA256 is:

f55ad803922380dc6b5db0cf6036d3bfc05be18bbbc7725257f29bf869320bbb

You can compare your downloaded ZIP with Get-FileHash -Algorithm SHA256. From this example's directory, open PowerShell, set the compiler path to your extracted bundle and run:

$sere = (Resolve-Path 'C:\tools\Sere-0.2.2\bin\sere.exe').Path
& $sere --version
& $sere build
if ($LASTEXITCODE -ne 0) { throw 'Sere build failed' }
& .\bin\native_math_example.exe
if ($LASTEXITCODE -ne 0) { throw 'Native example failed' }

Adjust the example compiler path to the actual extracted location. Use a terminal without another Sere project environment activated. Expected program output:

42
-5
42

How the files connect

File Purpose
libs/native/numbers.cpp Defines example_add and example_twice with C linkage and int32_t arguments/results.
src/main.sere Declares both native symbols with matching i32 arguments/results, then calls them, including a negative input. The extern declarations have no body.
sere.toml Sets the entry file and requests an eager native build with [build] native = true.

For example, this declaration binds the Sere name add to the C symbol example_add:

extern "C" "example_add"
def add(left: i32, right: i32) -> i32

The C++ definition's name, argument order, widths and return type must match that declaration. extern "C" prevents C++ name mangling. The manifest's native = true setting builds the loose C++ source under libs/native/ before compiling the application and links its archive.

Keep these declarations directly in src/main.sere for this Sere 0.2.2 layout. Importing a separate binding module can trigger additional native-artifact discovery and duplicate symbols from an object and its archive. The direct-entry pattern follows the official native_add.sere example and uses the project's native build path. After editing numbers.cpp, run the same build and executable commands again.

These functions demonstrate a primitive interface with fixed inputs whose arithmetic fits signed 32-bit integers. Strings, objects, ownership, callbacks and C++ exceptions crossing the boundary need a separate interface design. The boxed module registration API is not needed here. For distributing a .slib, follow the official library guide; packing and consuming a package are outside this application's check.

Check the complete workflow

In PowerShell 7, from this directory:

.\Verify.ps1 -CompilerPath $sere

The check uses the supplied 0.2.2 compiler in four temporary projects. It starts with the complete application and performs all four steps in that same project:

  1. Build and run the actual executable; require 42, -5, 42 and exit code zero.
  2. Build and run again without removing its generated artifacts; require the same results.
  3. Change the private C++ copy's multiplication from two to three, rebuild, and require 42, -5, 63.
  4. Restore the original C++ bytes, rebuild, and require 42, -5, 42 again.

Three fresh projects then disable native compilation, omit the C++ source, or remove both C-linkage declarations. Each must fail at the actual native symbols and produce no application. These controls apply to this direct-entry layout; the imported-module discovery path is different.

Only the three source inputs are copied, so an earlier build's archives and executables cannot satisfy a check. Your original application stays untouched. The script temporarily clears the documented Sere project/stdlib environment overrides, gives each case its own process temporary directory, restores the previous environment values on exit, and removes only its own temporary files. Its final JSON lists the cases that actually completed; a failed build, wrong output or unexpected successful omission stops the check.

This is a Windows x64 / Sere 0.2.2 verification target. Other compiler versions and platforms need their own native integration check.

References: typed extern extensions, native library layout, manifest settings, and the official native C++ / Sere binding examples. Sere and its portable bundle retain their upstream license.

Optional coffee

This example is free. If it saves you time, an entirely optional coffee is welcome:

  • USDC / SOL · Solana: 9tY6D9mwcFaJwwzEHvw2v7nhSpdjqjNBYtuooyBN6rYy
  • USDC / ETH · Base: 0x568Ab98578d682FB0B0b45619BE73EbFfbf5a6eA
  • USDT · BNB Smart Chain (BEP20): 0x568Ab98578d682FB0B0b45619BE73EbFfbf5a6eA