CINXE.COM

C interop using dart:ffi | Dart

<!doctype html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><script>!function(e,t,a,n){e[n]=e[n]||[],e[n].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var g=t.getElementsByTagName(a)[0],m=t.createElement(a);m.async=!0,m.src="https://www.googletagmanager.com/gtm.js?id=GTM-5VSZM5J",g.parentNode.insertBefore(m,g)}(window,document,"script","dataLayer")</script><meta name="description" content="To use C code in your Dart program, use the dart:ffi library."><title>C interop using dart:ffi | Dart</title><link rel="icon" sizes="64x64" href="/assets/img/logo/dart-64.png" eleventy:ignore><link href="/assets/img/touch-icon-iphone.png" rel="apple-touch-icon" eleventy:ignore><link href="/assets/img/touch-icon-ipad.png" rel="apple-touch-icon" sizes="152x152" eleventy:ignore><link href="/assets/img/touch-icon-iphone-retina.png" rel="apple-touch-icon" sizes="180x180" eleventy:ignore><link href="/assets/img/touch-icon-ipad-retina.png" rel="apple-touch-icon" sizes="167x167" eleventy:ignore><meta name="twitter:card" content="summary"><meta name="twitter:site" content="@dart_lang"><meta name="twitter:title" content="C interop using dart:ffi"><meta name="twitter:description" content="To use C code in your Dart program, use the dart:ffi library."><meta property="og:title" content="C interop using dart:ffi"><meta property="og:description" content="To use C code in your Dart program, use the dart:ffi library."><meta property="og:url" content="/interop/c-interop/"><meta property="og:image" content="/assets/img/logo/dart-logo-for-shares.png?2" eleventy:ignore><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Google+Sans:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Google+Sans+Display:wght@400&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Google+Sans+Mono:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Google+Sans+Text:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0..1,0" rel="stylesheet"><link rel="stylesheet" href="/assets/css/main.css?v=2"><script src="/assets/js/os-tabs.js?v=2"></script><script src="/assets/js/main.js?v=2"></script><script>!function(e,a,t,n,c,o,s){e.GoogleAnalyticsObject=c,e[c]=e[c]||function(){(e[c].q=e[c].q||[]).push(arguments)},e[c].l=1*new Date,o=a.createElement(t),s=a.getElementsByTagName(t)[0],o.async=1,o.src="//www.google-analytics.com/analytics.js",s.parentNode.insertBefore(o,s)}(window,document,"script",0,"ga"),ga("create","UA-26406144-4","auto"),ga("send","pageview")</script></head><body class="default.html"><a id="skip" href="#site-content-title" tabindex="1">Skip to main content</a><section id="cookie-notice"><div class="container"><p>dart.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.</p><div class="button-group"><a class="text-button" href="https://policies.google.com/technologies/cookies" target="_blank" rel="noopener">Learn more</a> <button id="cookie-consent" class="filled-button">OK, got it</button></div></div></section><noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5VSZM5J" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript><div id="site-banner" role="alert"><p>Announcing Dart 3.7! Find out about updates to the language, analyzer, pub.dev, and more, in the <a href="https://medium.com/dartlang/announcing-dart-3-7-bf864a1b195c" target="_blank">blog post</a>.</p></div><header id="page-header" class="site-header"><nav id="mainnav" class="site-header"><div id="menu-toggle"><span class="material-symbols" title="Toggle side navigation menu." aria-label="Toggle side navigation menu." type="button">menu</span></div><a href="/" class="brand" title="Dart"><img src="/assets/img/logo/logo-white-text.svg" alt="Dart"></a><ul class="navbar"><li><a href="/overview" class="nav-link">Overview</a></li><li class="mainnav__get-started"><a href="/docs" class="nav-link"><span>Docs</span></a></li><li><a href="/community" class="nav-link">Community</a></li><li><a href="/#try-dart" class="nav-link">Try Dart</a></li><li><a href="/get-dart" class="nav-link">Get Dart</a></li><li class="searchfield"><form action="/search" class="site-header__search form-inline" id="cse-search-box"><input type="hidden" name="cx" value="011220921317074318178:_yy-tmb5t_i"> <input type="hidden" name="ie" value="UTF-8"> <input type="hidden" name="hl" value="en"> <input class="site-header__searchfield form-control search-field" type="search" name="q" id="search-main" autocomplete="off" placeholder="Search" aria-label="Search"></form></li></ul></nav></header><div id="site-below-header"><div id="site-main-row"><div id="sidenav"><form action="/search/" class="site-header__search form-inline"><input class="site-header__searchfield form-control search-field" type="search" name="q" id="search-side" autocomplete="off" placeholder="Search" aria-label="Search"></form><ul class="navbar-nav"><li aria-hidden="true"><div class="sidenav-divider"></div></li><li class="nav-item"><a href="/overview" class="nav-link">Overview</a></li><li class="nav-item"><a href="/community" class="nav-link">Community</a></li><li class="nav-item"><a href="https://dartpad.dev" class="nav-link">Try Dart</a></li><li class="nav-item"><a href="/get-dart" class="nav-link">Get Dart</a></li><li class="nav-item"><a href="/docs" class="nav-link">Docs</a></li><li aria-hidden="true"><div class="sidenav-divider"></div></li></ul><ul class="nav"><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-1" role="button" aria-expanded="false" aria-controls="-sidenav-1"><span>Language</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1"><li class="nav-item"><a class="nav-link" href="/language"><div><span>Introduction</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-2" role="button" aria-expanded="false" aria-controls="-sidenav-1-2"><span>Syntax basics</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-2"><li class="nav-item"><a class="nav-link" href="/language/variables"><div><span>Variables</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/operators"><div><span>Operators</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/comments"><div><span>Comments</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/metadata"><div><span>Metadata</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/libraries"><div><span>Libraries & imports</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/keywords"><div><span>Keywords</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-3" role="button" aria-expanded="false" aria-controls="-sidenav-1-3"><span>Types</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-3"><li class="nav-item"><a class="nav-link" href="/language/built-in-types"><div><span>Built-in types</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/records"><div><span>Records</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/collections"><div><span>Collections</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/generics"><div><span>Generics</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/typedefs"><div><span>Typedefs</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/type-system"><div><span>Type system</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-4" role="button" aria-expanded="false" aria-controls="-sidenav-1-4"><span>Patterns</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-4"><li class="nav-item"><a class="nav-link" href="/language/patterns"><div><span>Overview & usage</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/pattern-types"><div><span>Pattern types</span></div></a></li><li class="nav-item"><a class="nav-link" href="https://codelabs.developers.google.com/codelabs/dart-patterns-records" target="_blank" rel="noopener"><div><span>Applied tutorial</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li></ul></li><li class="nav-item"><a class="nav-link" href="/language/functions"><div><span>Functions</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-6" role="button" aria-expanded="false" aria-controls="-sidenav-1-6"><span>Control flow</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-6"><li class="nav-item"><a class="nav-link" href="/language/loops"><div><span>Loops</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/branches"><div><span>Branches</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/error-handling"><div><span>Error handling</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-7" role="button" aria-expanded="false" aria-controls="-sidenav-1-7"><span>Classes & objects</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-7"><li class="nav-item"><a class="nav-link" href="/language/classes"><div><span>Classes</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/constructors"><div><span>Constructors</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/methods"><div><span>Methods</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/extend"><div><span>Extend a class</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/mixins"><div><span>Mixins</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/enums"><div><span>Enums</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/extension-methods"><div><span>Extension methods</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/extension-types"><div><span>Extension types</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/callable-objects"><div><span>Callable objects</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-8" role="button" aria-expanded="false" aria-controls="-sidenav-1-8"><span>Class modifiers</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-8"><li class="nav-item"><a class="nav-link" href="/language/class-modifiers"><div><span>Overview & usage</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/class-modifiers-for-apis"><div><span>Class modifiers for API maintainers</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/modifier-reference"><div><span>Reference</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-9" role="button" aria-expanded="false" aria-controls="-sidenav-1-9"><span>Concurrency</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-9"><li class="nav-item"><a class="nav-link" href="/language/concurrency"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/async"><div><span>Asynchronous support</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/isolates"><div><span>Isolates</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-10" role="button" aria-expanded="false" aria-controls="-sidenav-1-10"><span>Null safety</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-10"><li class="nav-item"><a class="nav-link" href="/null-safety"><div><span>Sound null safety</span></div></a></li><li class="nav-item"><a class="nav-link" href="/null-safety/migration-guide"><div><span>Migrating to null safety</span></div></a></li><li class="nav-item"><a class="nav-link" href="/null-safety/understanding-null-safety"><div><span>Understanding null safety</span></div></a></li><li class="nav-item"><a class="nav-link" href="/null-safety/unsound-null-safety"><div><span>Unsound null safety</span></div></a></li><li class="nav-item"><a class="nav-link" href="/null-safety/faq"><div><span>FAQ</span></div></a></li></ul></li></ul></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-2" role="button" aria-expanded="false" aria-controls="-sidenav-2"><span>Core libraries</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-2"><li class="nav-item"><a class="nav-link" href="/libraries"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-core"><div><span>dart:core</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-async"><div><span>dart:async</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-math"><div><span>dart:math</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-convert"><div><span>dart:convert</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-io"><div><span>dart:io</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop"><div><span>dart:js_interop</span></div></a></li><div class="sidenav-divider"></div><li class="nav-item"><a class="nav-link" href="/libraries/collections/iterables"><div><span>Iterable collections</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-2-10" role="button" aria-expanded="false" aria-controls="-sidenav-2-10"><span>Asynchronous programming</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-2-10"><li class="nav-item"><a class="nav-link" href="/libraries/async/async-await"><div><span>Tutorial</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/async/futures-error-handling"><div><span>Futures and error handling</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/async/using-streams"><div><span>Using streams</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/async/creating-streams"><div><span>Creating streams</span></div></a></li></ul></li></ul></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-3" role="button" aria-expanded="false" aria-controls="-sidenav-3"><span>Effective Dart</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-3"><li class="nav-item"><a class="nav-link" href="/effective-dart"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/style"><div><span>Style</span></div></a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/documentation"><div><span>Documentation</span></div></a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/usage"><div><span>Usage</span></div></a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/design"><div><span>Design</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-4" role="button" aria-expanded="false" aria-controls="-sidenav-4"><span>Packages</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-4"><li class="nav-item"><a class="nav-link" href="/tools/pub/packages"><div><span>How to use packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/useful-packages"><div><span>Commonly used packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/create-packages"><div><span>Creating packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/publishing"><div><span>Publishing packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/writing-package-pages"><div><span>Writing package pages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/workspaces"><div><span>Workspaces (monorepo support)</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-4-7" role="button" aria-expanded="false" aria-controls="-sidenav-4-7"><span>Package reference</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-4-7"><li class="nav-item"><a class="nav-link" href="/tools/pub/dependencies"><div><span>Dependencies</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/glossary"><div><span>Glossary</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/package-layout"><div><span>Package layout conventions</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/environment-variables"><div><span>Pub environment variables</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/pubspec"><div><span>Pubspec file</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/troubleshoot"><div><span>Troubleshooting pub</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/verified-publishers"><div><span>Verified publishers</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/security-advisories"><div><span>Security advisories</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/versioning"><div><span>Versioning</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/custom-package-repositories"><div><span>Custom package repositories</span></div></a></li></ul></li><li class="nav-item"><a class="nav-link" href="/tools/pub/private-files"><div><span>What not to commit</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-5" role="button" aria-expanded="false" aria-controls="-sidenav-5"><span>Development</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-5"><li class="nav-item"><a class="nav-link" href="/libraries/serialization/json"><div><span>JSON serialization</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/language/number-representation"><div><span>Number representation</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/google-apis"><div><span>Google APIs</span></div></a></li><li class="nav-item"><a class="nav-link" href="/multiplatform-apps"><div><span>Multi-platform apps</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-5-5" role="button" aria-expanded="false" aria-controls="-sidenav-5-5"><span>Command-line & server apps</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-5-5"><li class="nav-item"><a class="nav-link" href="/server"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/get-started"><div><span>Get started</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/cmdline"><div><span>Write command-line apps</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/fetch-data"><div><span>Fetch data from the internet</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/httpserver"><div><span>Write HTTP servers</span></div></a></li><li class="nav-item"><a class="nav-link" href="/server/libraries"><div><span>Libraries & packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/server/google-cloud"><div><span>Google Cloud</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-5-6" role="button" aria-expanded="false" aria-controls="-sidenav-5-6"><span>Web apps</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-5-6"><li class="nav-item"><a class="nav-link" href="/web"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/web/get-started"><div><span>Get started</span></div></a></li><li class="nav-item"><a class="nav-link" href="/web/deployment"><div><span>Deployment</span></div></a></li><li class="nav-item"><a class="nav-link" href="/web/libraries"><div><span>Libraries & packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/web/wasm"><div><span>Wasm compilation</span></div></a></li></ul></li><li class="nav-item"><a class="nav-link" href="/libraries/core/environment-declarations"><div><span>Environment declarations</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link active collapsible" data-toggle="collapse" data-target="#-sidenav-6" role="button" aria-expanded="true" aria-controls="-sidenav-6"><span>Interoperability</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse show" id="-sidenav-6"><li class="nav-item"><a class="nav-link active" href="/interop/c-interop"><div><span>C interop</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/objective-c-interop"><div><span>Objective-C & Swift interop</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/java-interop"><div><span>Java & Kotlin interop</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-6-4" role="button" aria-expanded="false" aria-controls="-sidenav-6-4"><span>JavaScript interop</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-6-4"><li class="nav-item"><a class="nav-link" href="/interop/js-interop"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/usage"><div><span>Usage</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/js-types"><div><span>JS types</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/tutorials"><div><span>Tutorials</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/past-js-interop"><div><span>Past JS interop</span></div></a></li><div class="sidenav-divider"></div><li class="nav-item"><a class="nav-link" href="/interop/js-interop/package-web"><div><span>Web interop</span></div></a></li></ul></li></ul></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-7" role="button" aria-expanded="false" aria-controls="-sidenav-7"><span>Tools & techniques</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-7"><li class="nav-item"><a class="nav-link" href="/tools"><div><span>Overview</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-7-2" role="button" aria-expanded="false" aria-controls="-sidenav-7-2"><span>Editors & debuggers</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-7-2"><li class="nav-item"><a class="nav-link" href="/tools/jetbrains-plugin"><div><span>IntelliJ & Android Studio</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/vs-code"><div><span>VS Code</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-devtools"><div><span>Dart DevTools</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-7-2-4" role="button" aria-expanded="false" aria-controls="-sidenav-7-2-4"><span>DartPad</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-7-2-4"><li class="nav-item"><a class="nav-link" href="/tools/dartpad"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dartpad/troubleshoot"><div><span>Troubleshooting DartPad</span></div></a></li></ul></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-7-3" role="button" aria-expanded="false" aria-controls="-sidenav-7-3"><span>Command-line tools</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-7-3"><li class="nav-item"><button class="nav-link collapsible" data-toggle="collapse" data-target="#-sidenav-7-3-1" role="button" aria-expanded="true" aria-controls="-sidenav-7-3-1"><span>Dart SDK</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse show" id="-sidenav-7-3-1"><li class="nav-item"><a class="nav-link" href="/tools/sdk"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-tool"><div><span>dart</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-analyze"><div><span>dart analyze</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-compile"><div><span>dart compile</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-create"><div><span>dart create</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-doc"><div><span>dart doc</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-fix"><div><span>dart fix</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-format"><div><span>dart format</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-info"><div><span>dart info</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/cmd"><div><span>dart pub</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-run"><div><span>dart run</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-test"><div><span>dart test</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dartaotruntime"><div><span>dartaotruntime</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/experiment-flags"><div><span>Experiment flags</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible" data-toggle="collapse" data-target="#-sidenav-7-3-2" role="button" aria-expanded="true" aria-controls="-sidenav-7-3-2"><span>Other command-line tools</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse show" id="-sidenav-7-3-2"><li class="nav-item"><a class="nav-link" href="/tools/build_runner"><div><span>build_runner</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/webdev"><div><span>webdev</span></div></a></li></ul></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-7-4" role="button" aria-expanded="false" aria-controls="-sidenav-7-4"><span>Static analysis</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-7-4"><li class="nav-item"><a class="nav-link" href="/tools/analysis"><div><span>Customizing static analysis</span></div></a></li><li class="nav-item"><a class="nav-link" href="/deprecated/sound-problems"><div><span>Fixing common type problems</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/non-promotion-reasons"><div><span>Fixing type promotion failures</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/linter-rules"><div><span>Linter rules</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/diagnostic-messages"><div><span>Diagnostic messages</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-7-5" role="button" aria-expanded="false" aria-controls="-sidenav-7-5"><span>Testing & optimization</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-7-5"><li class="nav-item"><a class="nav-link" href="/tools/testing"><div><span>Testing</span></div></a></li><li class="nav-item"><a class="nav-link" href="/web/debugging"><div><span>Debugging web apps</span></div></a></li></ul></li></ul></li><li aria-hidden="true"><div class="sidenav-divider"></div></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-9" role="button" aria-expanded="false" aria-controls="-sidenav-9"><span>Resources</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-9"><li class="nav-item"><a class="nav-link" href="/resources/dart-cheatsheet"><div><span>Language cheatsheet</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/breaking-changes"><div><span>Breaking changes</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/language/evolution"><div><span>Language evolution</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/language/spec"><div><span>Language specification</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/dart-3-migration"><div><span>Dart 3 migration guide</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-9-6" role="button" aria-expanded="false" aria-controls="-sidenav-9-6"><span>Coming from ...</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-9-6"><li class="nav-item"><a class="nav-link" href="/resources/coming-from/js-to-dart"><div><span>JavaScript to Dart</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/coming-from/swift-to-dart"><div><span>Swift to Dart</span></div></a></li></ul></li><div class="sidenav-divider"></div><li class="nav-item"><a class="nav-link" href="/resources/faq"><div><span>FAQ</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/glossary"><div><span>Glossary</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/books"><div><span>Books</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/videos"><div><span>Videos</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tutorials"><div><span>Tutorials</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible" data-toggle="collapse" data-target="#-sidenav-10" role="button" aria-expanded="true" aria-controls="-sidenav-10"><span>Related sites</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse show" id="-sidenav-10"><li class="nav-item"><a class="nav-link" href="https://api.dart.dev" target="_blank" rel="noopener"><div><span>API reference</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li><li class="nav-item"><a class="nav-link" href="https://medium.com/dartlang" target="_blank" rel="noopener"><div><span>Blog</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li><li class="nav-item"><a class="nav-link" href="https://dartpad.dev" target="_blank" rel="noopener"><div><span>DartPad (online editor)</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li><li class="nav-item"><a class="nav-link" href="https://flutter.dev" target="_blank" rel="noopener"><div><span>Flutter</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li><li class="nav-item"><a class="nav-link" href="https://pub.dev" target="_blank" rel="noopener"><div><span>Package site</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li></ul></li></ul></div><main id="page-content"><div id="site-toc--side" class="site-toc"><header class="site-toc__title">Contents</header><ul class="section-nav"><li class="toc-entry nav-item"><a class="nav-link" href="#download-example-files">Download example files</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#review-the-hello_world-example">Review the hello_<wbr>world example</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#bundle-and-load-c-libraries">Bundle and load C libraries</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#interface-with-native-types">Interface with native types</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#can-instantiate-these-type-signature-markers">Can instantiate these type signature markers</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#serve-as-type-signature-markers-only">Serve as type signature markers only</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#generate-ffi-bindings-with-package-ffigen">Generate FFI bindings with package:ffigen</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#native-assets">Build and bundle native assets</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#review-the-native_add_library-example">Review the native_<wbr>add_<wbr>library example</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#review-native-asset-api-documentation">Review Native Asset API documentation</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#opt-in-to-the-experiment">Opt-in to the experiment</a></li></ul></li></ul></div><article><div class="content"><div id="site-content-title"><h1>C interop using dart:ffi</h1></div><div id="site-toc--inline" class="site-toc toc-collapsible toc-collapsed"><header class="site-toc__title">Contents <span class="site-toc--inline__toggle toc-toggle-down"><i class="material-symbols">keyboard_arrow_down</i></span> <span class="site-toc--inline__toggle toc-toggle-up"><i class="material-symbols">keyboard_arrow_up</i></span></header><ul class="section-nav"><li class="toc-entry"><a href="#download-example-files">Download example files</a><ul><li class="toc-entry"><a href="#review-the-hello_world-example">Review the hello_world example</a></li></ul></li><li class="toc-entry"><a href="#bundle-and-load-c-libraries">Bundle and load C libraries</a></li><li class="toc-entry"><a href="#interface-with-native-types">Interface with native types</a><ul><li class="toc-entry"><a href="#can-instantiate-these-type-signature-markers">Can instantiate these type signature markers</a></li><li class="toc-entry"><a href="#serve-as-type-signature-markers-only">Serve as type signature markers only</a></li></ul></li><li class="toc-entry"><a href="#generate-ffi-bindings-with-package-ffigen">Generate FFI bindings with package:ffigen</a></li><li class="toc-entry"><a href="#native-assets">Build and bundle native assets</a><ul><li class="toc-entry"><a href="#review-the-native_add_library-example">Review the native_add_library example</a></li><li class="toc-entry"><a href="#review-native-asset-api-documentation">Review Native Asset API documentation</a></li><li class="toc-entry"><a href="#opt-in-to-the-experiment">Opt-in to the experiment</a></li></ul></li></ul><span class="site-toc--inline__toggle toc-toggle-more-items"><i class="material-symbols">more_horiz</i></span></div><p>Dart mobile, command-line, and server apps running on the <a href="/overview#platform">Dart Native platform</a> can use the <code>dart:ffi</code> library to call native C APIs, and to read, write, allocate, and deallocate native memory. <em>FFI</em> stands for <a href="https://en.wikipedia.org/wiki/Foreign_function_interface"><em>foreign function interface.</em></a> Other terms for similar functionality include <em>native interface</em> and <em>language bindings.</em></p><p>API documentation is available in the <a href="https://api.dart.dev/dart-ffi/dart-ffi-library.html"><code>dart:ffi</code> API reference.</a></p><div class="header-wrapper"><h2 id="download-example-files">Download example files</h2><a class="heading-link" href="#download-example-files" aria-label="Link to 'Download example files' section">#</a></div><p>To work with the examples in this guide, download the full <a href="https://github.com/dart-lang/samples/tree/main/ffi">ffi samples</a> directory. It includes the following examples show how to use the <code>dart:ffi</code> library:</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th><strong>Example</strong></th><th><strong>Description</strong></th></tr></thead><tbody><tr><td><a href="https://github.com/dart-lang/samples/tree/main/ffi/hello_world">hello_world</a></td><td>How to call a C function with no arguments and no return value.</td></tr><tr><td><a href="https://github.com/dart-lang/samples/tree/main/ffi/primitives">primitives</a></td><td>How to call C functions that have arguments and return values that are <strong>ints or pointers</strong>.</td></tr><tr><td><a href="https://github.com/dart-lang/samples/tree/main/ffi/structs">structs</a></td><td>How to use structs to pass <strong>strings</strong> to and from C and to handle <strong>simple and complex C structures</strong>.</td></tr><tr><td><a href="https://github.com/dart-lang/samples/tree/main/ffi/test_utils">test_utils</a></td><td>Common testing utilities for all of these examples.</td></tr></tbody></table></div><div class="header-wrapper"><h3 id="review-the-hello_world-example">Review the hello_world example</h3><a class="heading-link" href="#review-the-hello_world-example" aria-label="Link to 'Review the hello_world example' section">#</a></div><p>The <a href="https://github.com/dart-lang/samples/tree/main/ffi/hello_world">hello_world example</a> has the minimum necessary code for calling a C library. This example can be found in the <code>samples/ffi</code> you downloaded in the previous section.</p><div class="header-wrapper"><h4 id="files">Files</h4><a class="heading-link" href="#files" aria-label="Link to 'Files' section">#</a></div><p>The <code>hello_world</code> example has the following files:</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th><strong>Source file</strong></th><th><strong>Description</strong></th></tr></thead><tbody><tr><td><a href="https://github.com/dart-lang/samples/tree/main/ffi/hello_world/hello.dart"><code>hello.dart</code></a></td><td>A Dart file that uses the <code>hello_world()</code> function from a C library.</td></tr><tr><td><a href="https://github.com/dart-lang/samples/tree/main/ffi/hello_world/pubspec.yaml"><code>pubspec.yaml</code></a></td><td>The Dart <a href="/tools/pub/pubspec">pubspec</a> file, with an SDK lower bound of 3.4.</td></tr><tr><td><a href="https://github.com/dart-lang/samples/tree/main/ffi/hello_world/hello_library/hello.h"><code>hello_library/hello.h</code></a></td><td>Declares the <code>hello_world()</code> function.</td></tr><tr><td><a href="https://github.com/dart-lang/samples/tree/main/ffi/hello_world/hello_library/hello.c"><code>hello_library/hello.c</code></a></td><td>A C file that imports <code>hello.h</code> and defines the <code>hello_world()</code> function.</td></tr><tr><td><a href="https://github.com/dart-lang/samples/tree/main/ffi/hello_world/hello_library/hello.def"><code>hello_library/hello.def</code></a></td><td>A module-definition file which specifies information used when building a DLL.</td></tr><tr><td><a href="https://github.com/dart-lang/samples/tree/main/ffi/hello_world/hello_library/CMakeLists.txt"><code>hello_library/CMakeLists.txt</code></a></td><td>A CMake build file for compiling the C code into a dynamic library.</td></tr></tbody></table></div><p>Building the C library creates several files, including a dynamic library file named <code>libhello.dylib</code> (macOS), <code>libhello.dll</code> (Windows), or <code>libhello.so</code> (Linux).</p><div class="header-wrapper"><h4 id="build-and-execute">Build and execute</h4><a class="heading-link" href="#build-and-execute" aria-label="Link to 'Build and execute' section">#</a></div><p>The commands to build the dynamic library and execute the Dart app would resemble the following series.</p><div class="code-block-wrapper language-console"><div class="code-block-body"><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#222222">$ cd hello_library</span></span> <span class="line"><span style="color:#222222">$ cmake </span><span style="color:#6200EE">.</span></span> <span class="line"><span style="color:#222222">...</span></span> <span class="line"><span style="color:#222222">$ make</span></span> <span class="line"><span style="color:#222222">...</span></span> <span class="line"><span style="color:#222222">$ cd ..</span></span> <span class="line"><span style="color:#222222">$ dart pub get</span></span> <span class="line"><span style="color:#222222">$ dart run hello.dart</span></span> <span class="line"><span style="color:#222222">Hello World</span></span></code></pre></div></div><aside class="alert alert-info"><div class="alert-header"><span class="material-symbols" aria-hidden="true">info</span> <span>Note</span></div><div class="alert-content"><p><strong>On macOS,</strong> executables, including the Dart VM (<code>dart</code>), can load only <strong>signed libraries.</strong> To learn more about signing libraries, consult Apple's <a href="https://developer.apple.com/library/content/documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html">Code Signing Guide.</a></p></div></aside><div class="header-wrapper"><h4 id="leverage-dart-ffi">Leverage dart:ffi</h4><a class="heading-link" href="#leverage-dart-ffi" aria-label="Link to 'Leverage dart:ffi' section">#</a></div><p>To learn how to call a C function using the <code>dart:ffi</code> library, review the <a href="https://github.com/dart-lang/samples/tree/main/ffi/hello_world/hello.dart"><code>hello.dart</code> file</a>. This section explains the contents of this file.</p><ol><li><p>Import <code>dart:ffi</code>.</p><div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">import</span><span style="color:#11796D"> 'dart:ffi'</span><span style="color:#D43324"> as</span><span style="color:#222222"> ffi;</span></span></code></pre></div></div></li><li><p>Import the path library that you'll use to store the path of dynamic library.</p><div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">import</span><span style="color:#11796D"> 'dart:io'</span><span style="color:#D43324"> show</span><span style="color:#222222"> Platform, Directory;</span></span> <span class="line"><span style="color:#D43324">import</span><span style="color:#11796D"> 'package:path/path.dart'</span><span style="color:#D43324"> as</span><span style="color:#222222"> path;</span></span></code></pre></div></div></li><li><p>Create a typedef with the FFI type signature of the C function.<br>To learn about the most used types according to the <code>dart:ffi</code> library consult <a href="#interface-with-native-types">Interfacing with native types</a>.</p><div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">typedef</span><span style="color:#222222"> hello_world_func = ffi.</span><span style="color:#0468D7">Void</span><span style="color:#0468D7"> Function</span><span style="color:#222222">();</span></span></code></pre></div></div></li><li><p>Create a <code>typedef</code> for the variable to use when calling the C function.</p><div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">typedef</span><span style="color:#0468D7"> HelloWorld</span><span style="color:#222222"> = </span><span style="color:#D43324">void</span><span style="color:#0468D7"> Function</span><span style="color:#222222">();</span></span></code></pre></div></div></li><li><p>Create a variable to store the path of the dynamic library.</p><div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> libraryPath = path.</span><span style="color:#6200EE">join</span><span style="color:#222222">(</span><span style="color:#0468D7">Directory</span><span style="color:#222222">.current.path, </span><span style="color:#11796D">'hello_library'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#11796D"> 'libhello.so'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">if</span><span style="color:#222222"> (</span><span style="color:#0468D7">Platform</span><span style="color:#222222">.isMacOS) {</span></span> <span class="line"><span style="color:#222222"> libraryPath = path.</span><span style="color:#6200EE">join</span><span style="color:#222222">(</span><span style="color:#0468D7">Directory</span><span style="color:#222222">.current.path, </span><span style="color:#11796D">'hello_library'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#11796D"> 'libhello.dylib'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">} </span><span style="color:#D43324">else</span><span style="color:#D43324"> if</span><span style="color:#222222"> (</span><span style="color:#0468D7">Platform</span><span style="color:#222222">.isWindows) {</span></span> <span class="line"><span style="color:#222222"> libraryPath = path.</span><span style="color:#6200EE">join</span><span style="color:#222222">(</span><span style="color:#0468D7">Directory</span><span style="color:#222222">.current.path, </span><span style="color:#11796D">'hello_library'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#11796D"> 'Debug'</span><span style="color:#222222">, </span><span style="color:#11796D">'hello.dll'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div></li><li><p>Open the dynamic library that contains the C function.</p><div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> dylib = ffi.</span><span style="color:#0468D7">DynamicLibrary</span><span style="color:#222222">.</span><span style="color:#6200EE">open</span><span style="color:#222222">(libraryPath);</span></span></code></pre></div></div></li><li><p>Get a reference to the C function, and put it into a variable. This code uses the <code>typedefs</code> from steps 2 and 3, along with the dynamic library variable from step 4.</p><div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">final</span><span style="color:#0468D7"> HelloWorld</span><span style="color:#222222"> hello = dylib</span></span> <span class="line"><span style="color:#222222"> .lookup&#x3C;ffi.</span><span style="color:#0468D7">NativeFunction</span><span style="color:#222222">&#x3C;hello_world_func>>(</span><span style="color:#11796D">'hello_world'</span><span style="color:#222222">)</span></span> <span class="line"><span style="color:#222222"> .</span><span style="color:#6200EE">asFunction</span><span style="color:#222222">();</span></span></code></pre></div></div></li><li><p>Call the C function.</p><div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6200EE">hello</span><span style="color:#222222">();</span></span></code></pre></div></div></li></ol><p>Once you understand the <code>hello_world</code> example, consult the <a href="#download-example-files">other <code>dart:ffi</code> examples</a>.</p><div class="header-wrapper"><h2 id="bundle-and-load-c-libraries">Bundle and load C libraries</h2><a class="heading-link" href="#bundle-and-load-c-libraries" aria-label="Link to 'Bundle and load C libraries' section">#</a></div><p>The method to bundle / package / distribute then load a native C library depends on the platform and library type.</p><p>To learn how, consult the following pages and examples.</p><ul><li>Flutter <code>dart:ffi</code> for <a href="https://docs.flutter.dev/development/platform-integration/android/c-interop">Android</a> apps</li><li>Flutter <code>dart:ffi</code> for <a href="https://docs.flutter.dev/development/platform-integration/ios/c-interop">iOS</a> apps</li><li>Flutter <code>dart:ffi</code> for <a href="https://docs.flutter.dev/development/platform-integration/macos/c-interop">macOS</a> apps</li><li><a href="https://github.com/dart-lang/samples/tree/main/ffi"><code>dart:ffi</code> examples</a></li></ul><div class="header-wrapper"><h2 id="interface-with-native-types">Interface with native types</h2><a class="heading-link" href="#interface-with-native-types" aria-label="Link to 'Interface with native types' section">#</a></div><p>The <code>dart:ffi</code> library provides multiple types that implement <a href="https://api.dart.dev/dart-ffi/NativeType-class.html"><code>NativeType</code></a> and represent native types in C. You can instantiate some native types. Some other native types can be used only as markers in type signatures.</p><div class="header-wrapper"><h3 id="can-instantiate-these-type-signature-markers">Can instantiate these type signature markers</h3><a class="heading-link" href="#can-instantiate-these-type-signature-markers" aria-label="Link to 'Can instantiate these type signature markers' section">#</a></div><p>The following native types can be used as markers in type signatures. They or their subtypes <em>can</em> be instantiated in Dart code.</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th><strong>Dart type</strong></th><th><strong>Description</strong></th></tr></thead><tbody><tr><td><a href="https://api.dart.dev/dart-ffi/Array-class.html">Array</a></td><td>A fixed-sized array of items. Supertype of type specific arrays.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Pointer-class.html">Pointer</a></td><td>Represents a pointer into native C memory.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Struct-class.html">Struct</a></td><td>The supertype of all FFI struct types.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Union-class.html">Union</a></td><td>The supertype of all FFI union types.</td></tr></tbody></table></div><div class="header-wrapper"><h3 id="serve-as-type-signature-markers-only">Serve as type signature markers only</h3><a class="heading-link" href="#serve-as-type-signature-markers-only" aria-label="Link to 'Serve as type signature markers only' section">#</a></div><p>The following list shows which platform-agnostic native types that serve as markers in type signatures. They <em>can't</em> be instantiated in Dart code.</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th><strong>Dart type</strong></th><th><strong>Description</strong></th></tr></thead><tbody><tr><td><a href="https://api.dart.dev/dart-ffi/Bool-class.html">Bool</a></td><td>Represents a native bool in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Double-class.html">Double</a></td><td>Represents a native 64 bit double in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Float-class.html">Float</a></td><td>Represents a native 32 bit float in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Int8-class.html">Int8</a></td><td>Represents a native signed 8 bit integer in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Int16-class.html">Int16</a></td><td>Represents a native signed 16 bit integer in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Int32-class.html">Int32</a></td><td>Represents a native signed 32 bit integer in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Int64-class.html">Int64</a></td><td>Represents a native signed 64 bit integer in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/NativeFunction-class.html">NativeFunction</a></td><td>Represents a function type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Opaque-class.html">Opaque</a></td><td>The supertype of all opaque types in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Uint8-class.html">Uint8</a></td><td>Represents a native unsigned 8 bit integer in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Uint16-class.html">Uint16</a></td><td>Represents a native unsigned 16 bit integer in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Uint32-class.html">Uint32</a></td><td>Represents a native unsigned 32 bit integer in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Uint64-class.html">Uint64</a></td><td>Represents a native unsigned 64 bit integer in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Void-class.html">Void</a></td><td>Represents the <code>void</code> type in C.</td></tr></tbody></table></div><p>There are also many <a href="https://api.dart.dev/dart-ffi/Abi-class.html">ABI</a> specific marker native types that extend <a href="https://api.dart.dev/dart-ffi/AbiSpecificInteger-class.html">AbiSpecificInteger</a>. To learn how these types map on specific platforms, consult the API documentation linked in the following table.</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th><strong>Dart type</strong></th><th><strong>Description</strong></th></tr></thead><tbody><tr><td><a href="https://api.dart.dev/dart-ffi/AbiSpecificInteger-class.html">AbiSpecificInteger</a></td><td>The supertype of all ABI-specific integer types.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Int-class.html">Int</a></td><td>Represents the <code>int</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/IntPtr-class.html">IntPtr</a></td><td>Represents the <code>intptr_t</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Long-class.html">Long</a></td><td>Represents the <code>long int</code> (<code>long</code>) type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/LongLong-class.html">LongLong</a></td><td>Represents the <code>long long</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Short-class.html">Short</a></td><td>Represents the <code>short</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/SignedChar-class.html">SignedChar</a></td><td>Represents the <code>signed char</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/Size-class.html">Size</a></td><td>Represents the <code>size_t</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/UintPtr-class.html">UintPtr</a></td><td>Represents the <code>uintptr_t</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/UnsignedChar-class.html">UnsignedChar</a></td><td>Represents the <code>unsigned char</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/UnsignedInt-class.html">UnsignedInt</a></td><td>Represents the <code>unsigned int</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/UnsignedLong-class.html">UnsignedLong</a></td><td>Represents the <code>unsigned long int</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/UnsignedLongLong-class.html">UnsignedLongLong</a></td><td>Represents the <code>unsigned long long</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/UnsignedShort-class.html">UnsignedShort</a></td><td>Represents the <code>unsigned short</code> type in C.</td></tr><tr><td><a href="https://api.dart.dev/dart-ffi/WChar-class.html">WChar</a></td><td>Represents the <code>wchar_t</code> type in C.</td></tr></tbody></table></div><div class="header-wrapper"><h2 id="generate-ffi-bindings-with-package-ffigen">Generate FFI bindings with <code>package:ffigen</code></h2><a class="heading-link" href="#generate-ffi-bindings-with-package-ffigen" aria-label="Link to 'Generate FFI bindings with package:ffigen' section">#</a></div><p>For large API surfaces, it can be time-consuming to write the Dart bindings that integrate with the C code. To have Dart create FFI wrappers from C header files, use the <a href="https://pub.dev/packages/ffigen"><code>package:ffigen</code></a> binding generator.</p><div class="header-wrapper"><h2 id="native-assets">Build and bundle native assets</h2><a class="heading-link" href="#native-assets" aria-label="Link to 'Build and bundle native assets' section">#</a></div><aside class="alert alert-info"><div class="alert-header"><span class="material-symbols" aria-hidden="true">info</span> <span>Note</span></div><div class="alert-content"><p>The native assets are <strong>experimental</strong>, and <a href="https://github.com/dart-lang/sdk/issues/50565">in active development</a>.</p></div></aside><p>The <em>Native Assets</em> feature should resolve a number of issues associated with the distribution of Dart packages that depend on native code. It does so by providing uniform hooks for integrating with various build systems involved in building Flutter and standalone Dart applications.</p><p>This feature should simplify how Dart packages depend on and use native code. Native Assets should provide the following benefits:</p><ul><li>Build the native code or obtains the binaries using a package's <code>hook/build.dart</code> build hook.</li><li>Bundle the native <a href="https://pub.dev/documentation/native_assets_cli/latest/native_assets_cli/Asset-class.html"><code>Asset</code></a> that the <code>build.dart</code> build hook reports.</li><li>Make native assets available at runtime through declarative <code>@Native&lt;&gt;() extern</code> functions using the <a href="https://api.dart.dev/dart-ffi/Native/assetId.html"><code>assetId</code></a>.</li></ul><p>When you <a href="#opt-in-to-the-experiment">opt in</a> to the native experiment, The <code>flutter (run|build)</code> and <code>dart (run|build)</code> commands build and bundle native code with the Dart code.</p><div class="header-wrapper"><h3 id="review-the-native_add_library-example">Review the <code>native_add_library</code> example</h3><a class="heading-link" href="#review-the-native_add_library-example" aria-label="Link to 'Review the native_add_library example' section">#</a></div><p>The <a href="https://github.com/dart-lang/native/blob/main/pkgs/native_assets_cli/example/build/native_add_library"><code>native_add_library</code></a> example includes the minimum code to build and bundle C code in a Dart package.</p><p>The example includes the following files:</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th><strong>Source file</strong></th><th><strong>Description</strong></th></tr></thead><tbody><tr><td><a href="https://github.com/dart-lang/native/blob/main/pkgs/native_assets_cli/example/build/native_add_library/src/native_add_library.c"><code>src/native_add_library.c</code></a></td><td>The C file containing the code for <code>add</code>.</td></tr><tr><td><a href="https://github.com/dart-lang/native/blob/main/pkgs/native_assets_cli/example/build/native_add_library/lib/native_add_library.dart"><code>lib/native_add_library.dart</code></a></td><td>The Dart file that invokes the C function <code>add</code> in asset <code>package:native_add_library/native_add_library.dart</code> through FFI. (Note that <em>asset id</em> defaults to the library uri.)</td></tr><tr><td><a href="https://github.com/dart-lang/native/blob/main/pkgs/native_assets_cli/example/build/native_add_library/test/native_add_library_test.dart"><code>test/native_add_library_test.dart</code></a></td><td>A Dart test using the native code.</td></tr><tr><td><a href="https://github.com/dart-lang/native/blob/main/pkgs/native_assets_cli/example/build/native_add_library/hook/build.dart"><code>hook/build.dart</code></a></td><td>A build hook for compiling <code>src/native_add_library.c</code> and declaring the compiled asset with id <code>package:native_add_library/native_add_library.dart</code>.</td></tr></tbody></table></div><p>When a Dart or Flutter project depends on <code>package:native_add_library</code>, it invokes the <code>hook/build.dart</code> build hook on <code>run</code>, <code>build</code>, and <code>test</code> commands. The <a href="https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli/example/build/native_add_app"><code>native_add_app</code></a> example showcases a use of <code>native_add_library</code>.</p><div class="header-wrapper"><h3 id="review-native-asset-api-documentation">Review Native Asset API documentation</h3><a class="heading-link" href="#review-native-asset-api-documentation" aria-label="Link to 'Review Native Asset API documentation' section">#</a></div><p>API documentation can be found for the following packages:</p><ul><li>To learn about native assets in Dart FFI, consult the <code>dart:ffi</code> API reference for <a href="https://api.dart.dev/dart-ffi/Native-class.html"><code>Native</code></a> and <a href="https://api.dart.dev/dart-ffi/DefaultAsset-class.html"><code>DefaultAsset</code></a>.</li><li>To learn about the <code>hook/build.dart</code> build hook, consult the <a href="https://pub.dev/documentation/native_assets_cli/latest/"><code>package:native_assets_cli</code> API reference</a>.</li></ul><div class="header-wrapper"><h3 id="opt-in-to-the-experiment">Opt-in to the experiment</h3><a class="heading-link" href="#opt-in-to-the-experiment" aria-label="Link to 'Opt-in to the experiment' section">#</a></div><p>To learn how to enable the experiment and provide feedback, consult these tracking issues:</p><ul><li><a href="https://github.com/dart-lang/sdk/issues/50565">Dart native assets</a></li><li><a href="https://github.com/flutter/flutter/issues/129757">Flutter native assets</a></li></ul><p id="page-github-links"><span>Unless stated otherwise, the documentation on this site reflects Dart 3.7.0. Page last updated on 2024-11-17.</span> <a href="https://github.com/dart-lang/site-www/tree/main/src/content/interop/c-interop.md" target="_blank" rel="noopener">View source</a> <span>or </span><a href="https://github.com/dart-lang/site-www/issues/new?template=1_page_issue.yml&page-url=https://dart.dev/interop/c-interop/&page-source=https://github.com/dart-lang/site-www/tree/main/src/content/interop/c-interop.md" title="Report an issue with this page" target="_blank" rel="noopener">report an issue</a>.</p></div></article></main></div><footer id="page-footer"><div class="footer-section footer-main"><a href="/" class="brand" title="Dart"><img src="/assets/img/logo/logo-white-text.svg" alt="Dart" width="164"></a><div class="footer-social-links"><a href="https://medium.com/dartlang" target="_blank" rel="noopener" title="Dart's Medium publication"><svg><use href="/assets/img/social/medium.svg#medium"></use></svg> </a><a href="https://github.com/dart-lang" target="_blank" rel="noopener" title="Dart's GitHub organization"><svg><use href="/assets/img/social/github.svg#github"></use></svg> </a><a href="https://bsky.app/profile/dart.dev" target="_blank" rel="noopener" title="Dart's Bluesky profile"><svg><use href="/assets/img/social/bluesky.svg#bluesky"></use></svg> </a><a href="https://twitter.com/dart_lang" target="_blank" rel="noopener" title="Dart's X (Twitter) profile"><svg><use href="/assets/img/social/x.svg#x"></use></svg></a></div></div><div class="footer-section footer-tray"><div class="footer-licenses">Except as otherwise noted, this site is licensed under a <a href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>, and code samples are licensed under the <a href="https://opensource.org/licenses/BSD-3-Clause">3-Clause BSD License</a>.</div><div class="footer-utility-links"><ul><li><a href="/terms" title="Terms of use">Terms</a></li><li><a href="https://policies.google.com/privacy" target="_blank" rel="noopener" title="Privacy policy">Privacy</a></li><li><a href="/security" title="Security philosophy and practices">Security</a></li></ul></div></div></footer></div></body></html>

Pages: 1 2 3 4 5 6 7 8 9 10