Compiling C Code into Code Assets with Native Assets Hooks
Integrate and automate the compilation and packaging of native C/C++ source code into Code Assets under Dart's overarching Native Assets feature using build and link hooks.
Contents
- Introduction
- Constraints
- Native Interop Packages
- Step-by-Step Workflow
- Choosing an Integration Approach
- Method 1: Local Compilation with Linker Tree-Shaking (Recommended)
- Method 2: Downloading Precompiled Dynamic Libraries
- Verification Checklist
Introduction
Under Dart's Native Assets feature, packages can package native code (like C/C++ libraries) as Code Assets and bundle them automatically during standard development cycles (e.g., dart run, dart test, dart build, and flutter run). The packaging of Code Assets is driven by two programmatic hook scripts placed inside a package's hook/ folder:
hook/build.dart: Compiles local C sources to machine code or bundles prebuilt native binaries as code assets for a specific host/target architecture.hook/link.dart: Links built code assets, applying advanced tree-shaking optimizations to strip unused native symbols and compress the runtime binary size.
Constraints
[!IMPORTANT] Keep all file resolving platform-independent. Never hardcode absolute target paths, shell scripts, or system command variables. Always use
Platform.script.resolve()orUri-based resolution to ensure scripts are fully portable.
- Hook Locations: Compiling and packaging hooks must reside strictly inside the
hook/directory at the package's root:hook/build.dart(Build execution phase)hook/link.dart(Optional packaging/linking/tree-shaking phase)
- Compile Toolchain Standard: Use the programmatic APIs from
package:native_toolchain_c(e.g.CBuilderandCLibrary) to run compile toolchains. Never invoke rawgcc,clang, ormsvcvia shell commands. - Preamble & License Headers: Every handcrafted and generated source file (including bindings, helpers, and hooks) must strictly contain the target package's copyright and licensing header.
- Tree Shaking Mapping: If utilizing compiler tree-shaking, you must map the target Dart method names (e.g.
Method.name) back to their raw native C symbol names using a record use mapping generated by FFIgen. The mapping file must reside underlib/src/third_party/and strictly use the.g.dartextension (e.g.,sqlite3.record_use_mapping.g.dart). - Integrity Safeguards for Precompiled Libraries: If adopting the dynamic download pattern:
- Cryptographic Verification: Downloaded prebuilt binaries must be checked against preconfigured lookup tables containing MD5 or SHA-256 hashes to guarantee binary integrity and prevent tampering.
- Graceful Recovery: Support offline developers by providing fallbacks (such as local compiler execution via flags like
local_build).
Native Interop Packages
Programmatic build and link hooks for Code Assets leverage three specialized native interop packages:
Step-by-Step Workflow
Step 1: Add Dependencies
Add Code Assets hook and toolchain dependencies to your package. You must fetch these dependencies directly from pub.dev.
You can add it automatically using the CLI:
Or manually declare them in your target package's pubspec.yaml:
Step 2: Define C Specifications
Define your target C library compilation metadata inside lib/src/c_library.dart. This lets both the build and link hooks share a single source of truth for assets, names, and sources.
Step 3: Implement Build and Link Hook Scripts
Write the compilation orchestration script inside hook/build.dart and the dead-code elimination logic inside hook/link.dart.
Step 4: Run the Hook Cycle
Running standard test suites dynamically launches the build and link hook lifecycle in the background:
Choosing an Integration Approach
There are two primary methods for integrating and delivering C/C++ native assets in Dart. Select the one that matches your project requirements:
Method 1: Local Compilation with Linker Tree-Shaking (Recommended)
In this approach, the build hook invokes local toolchains (GCC, Clang, MSVC) to compile source files directly. The link hook subsequently filters output symbols utilizing compiler options, retaining only target methods invoked in user code. This represents the standard, robust SQLite pattern under pkgs/code_assets/example/sqlite.
Prerequisite Host Compiler Toolchains
Since package:native_toolchain_c delegates actual dynamic compilation to the host operating system's default toolchain, the development machine must have one of the following compiler packages pre-installed:
- macOS: Xcode Command Line Tools. Install via:
- Linux: GCC or Clang. Install via:
- Windows: MSVC (Microsoft Visual C++). Install the Visual Studio Installer and select the Desktop development with C++ workload.
Note: If no compatible toolchain is discovered on the host path, the build hook script will throw a compilation execution exception. Ensure to specify compiler constraints or adopt Method 2 if toolchains cannot be guaranteed.
C Source and Bindings Setup
Assume a C source defining simple math functions at third_party/sqlite/sqlite3.c with its entry point header at third_party/sqlite/sqlite3.h:
We utilize a programmatic FFIgen script (tool/ffigen.dart) to create FFI bindings in lib/src/third_party/sqlite3.g.dart, enabling recorded usage tracking and producing the lookup metadata map in lib/src/third_party/sqlite3.record_use_mapping.g.dart:
Defining the C Library Build Spec
Define the centralized library specification in lib/src/c_library.dart:
Implementing hook/build.dart
Implement hook/build.dart using CLibrary.build. This builds the library to a dynamic library (e.g. .so, .dylib, or .dll) inside the hook's target directory:
Implementing hook/link.dart
Implement the link optimization phase in hook/link.dart. This utilizes compiler tree-shaking options (LinkerOptions.treeshake) to compile a minimized, dead-code-eliminated binary based on symbol usage records:
Method 2: Downloading Precompiled Dynamic Libraries
An alternative approach compiles binaries beforehand on a central build machine, archives them, and downloads the target binary during the build hook execution. This matches the paradigm demonstrated in the download_asset hook package.
Why Download Precompiled Binaries?
- Host Constraints: Compiling large C/C++ libraries locally requires a complete compiler setup (GCC, Xcode/SDKs, Visual Studio) that the end-developer's host machine may not possess.
- Compile Speed: Precompiled downloads execute in milliseconds compared to potentially long multi-minute compilation processes.
- Platform Bridging: Allows cross-compiling constraints to be avoided if host architectures are limited.
Implementing Precompiled Dynamic Downloads
We configure our build hook to detect local compiler flags (e.g. local_build). If not specified, the hook utilizes HttpClient to pull down platform-specific libraries, calculates the MD5 hash to confirm download safety against a configured hashes lookup table, and registers the binary file as a CodeAsset:
1. Defining Target Hashes (lib/src/hook_helpers/hashes.dart)
Define target MD5 hash checks per platform file in your package sources:
2. Hook Downloader Helper (lib/src/hook_helpers/download.dart)
Implement the downloading and integrity check logic using dynamic target filename matching:
3. Implementing hook/build.dart
Write the final download build hook incorporating local compilation fallback:
Verification Checklist
Before declaring a build or link hook implementation complete, always perform the following checks:
1. Local Execution Sandbox
Run unit tests and confirm the native assets compile/link process completes with no runtime or build tool exceptions:
2. Verify Target Outputs
Navigate to your package target directory and verify that dynamic binary assets are created for the host system:
- macOS: Verify
.dart_tool/resources/or target directories contain.dylibfiles. - Linux: Verify
.dart_tool/resources/or target directories contain.sofiles. - Windows: Verify
.dart_tool/resources/or target directories contain.dllfiles.
3. Verify Tree-Shaking Stripping
To ensure the link hook is actually stripping unused native symbols and compressing binary packaging, perform the following validation:
- Compile a production bundle of the CLI/app:
- Navigate to the compiled build directory containing the dynamic library.
- Query the exported dynamic symbol tables:
- macOS:
- Linux:
- Windows (using MSVC Developer Command Prompt):
- Confirm Target Exports: Verify that the command outputs only the explicitly kept entry point functions (e.g.
sqlite3_libversion) and does not output any unreferenced/stripped symbols. - No Bundle Scenario: If the application does not import or invoke any methods from the native library:
- Verify that the link hook logs:
Skipping linking as no symbols are to be kept. - Verify that no library was built/placed in the production bundle (the
.dylib/.so/.dllfile is not generated, saving bundle size).
- Verify that the link hook logs:
4. Verify Offline Compliance (User Defines)
Confirm offline compliance is fully active and the download fallback executes perfectly offline:
- Configure the
local_build: truedefine for your package in the package'spubspec.yaml(or the workspace rootpubspec.yaml): - Disable the machine's network adapter or run in a sandboxed offline shell.
- Launch unit tests:
- Verify the test suite successfully compiles local source files using host compilers, has no compile errors, and never attempts network download requests.

