#cargo #cdylib

bin+lib cargo-c

Helper program to build and install c-like libraries

14 releases (5 breaking)

✓ Uses Rust 2018 edition

new 0.6.2 Mar 22, 2020
0.5.3 Mar 16, 2020
0.5.1 Dec 17, 2019
0.3.1 Nov 16, 2019
0.2.1 Jul 10, 2019

#42 in Cargo plugins

Download history 719/week @ 2019-12-02 212/week @ 2019-12-09 324/week @ 2019-12-16 177/week @ 2019-12-23 139/week @ 2019-12-30 113/week @ 2020-01-06 157/week @ 2020-01-13 302/week @ 2020-01-20 199/week @ 2020-01-27 183/week @ 2020-02-03 205/week @ 2020-02-10 247/week @ 2020-02-17 264/week @ 2020-02-24 121/week @ 2020-03-02 160/week @ 2020-03-09 346/week @ 2020-03-16

869 downloads per month

MIT and MPL-2.0 licenses

862 lines

Cargo C-ABI helpers

LICENSE dependency status Crates.io Build Status

cargo applet to build and install C-ABI compatibile dynamic and static libraries.

It produces and installs a correct pkg-config file, a static library and a dynamic library, and a C header to be used by any C (and C-compatible) software.


# build the library, create the .h header, create the .pc file
$ cargo cbuild --destdir=${D} --prefix=/usr --libdir=/usr/lib64
# build the library, create the .h header, create the .pc file and install all of it
$ cargo cinstall --destdir=${D} --prefix=/usr --libdir=/usr/lib64

For a more in-depth explanation of how cargo-c works and how to use it for your crates, read Building Crates so they Look Like C ABI Libraries.

The TL;DR:

  • Create a capi.rs with the C-API you want to expose and use #[cfg(cargo_c)] to hide it when you build a normal rust library.
  • Make sure you have a lib target and if you are using a workspace the first member is the crate you want to export, that means that you might have to add a "." member at the start of the list.
  • Since Rust 1.38, also add "staticlib" to the "lib" crate-type.
  • Remember to add a cbindgen.toml and fill it with at least the include guard and probably you want to set the language to C (it defaults to C++)
  • Once you are happy with the result update your documentation to tell the user to install cargo-c and do cargo cinstall --prefix=/usr --destdir=/tmp/some-place or something along those lines.



  • cli
    • build command
    • install command
    • cargo applet support
  • build targets
    • pkg-config generation
    • header generation (cbindgen integration)
  • staticlib support
  • cdylib support
  • Generate version information in the header
    • Make it tunable
  • Extra Cargo.toml keys
  • Better status reporting


~756K SLoC