# Linking Library C with Rust Programm

**URL:** <https://users.rust-lang.org/t/linking-library-c-with-rust-programm/18228>\
**Category:** help\
**Created:** [June 21, 2018, 9:15am UTC](https://users.rust-lang.org/t/linking-library-c-with-rust-programm/18228 "2018-06-21T09:15:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![souf061](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@souf061](https://users.rust-lang.org/u/souf061)\
**Post date:** [June 21, 2018, 9:15am UTC](https://users.rust-lang.org/t/linking-library-c-with-rust-programm/18228/1 "2018-06-21T09:15:11Z")

</div>

Dear Rust users,

As part of a project, I have to use functions of a lib C in my program.  
I would like to link a dynamic library compiled with CMake with a program Rust.  
I need to compile my lib at the same time as my Rust program:  
Is there a simple way to build a program in Rust with cmake?

I’d be very happy if someone could tell me how to resolve the problem

---

<div class="post-metadata">

**Author:** ![sanxiyn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sanxiyn/32/24284_2.png) [@sanxiyn](https://users.rust-lang.org/u/sanxiyn)\
**Post date:** [June 21, 2018, 9:21am UTC](https://users.rust-lang.org/t/linking-library-c-with-rust-programm/18228/2 "2018-06-21T09:21:25Z")

</div>

> Is there a simple way to build a program in Rust with cmake?

Yes, you can use [cmake crate](https://crates.io/crates/cmake) for this.

---

<div class="post-metadata">

**Author:** ![wezm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/wezm/32/6720_2.png) [@wezm](https://users.rust-lang.org/u/wezm)\
**Post date:** [June 21, 2018, 11:28am UTC](https://users.rust-lang.org/t/linking-library-c-with-rust-programm/18228/3 "2018-06-21T11:28:56Z")

</div>

There was also an article posted recently, [Using Rust Code in a C/C++ Project with CMake](https://blog.devolutions.net/2018/06/insider-series-using-rust-code-in-a-cc-project-with-cmake) that might be useful.

---

<div class="post-metadata">

**Author:** ![souf061](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@souf061](https://users.rust-lang.org/u/souf061)\
**Post date:** [June 21, 2018, 2:29pm UTC](https://users.rust-lang.org/t/linking-library-c-with-rust-programm/18228/4 "2018-06-21T14:29:07Z")

</div>

Thanks ,  
I try to use the example in : [cmake - Rust](https://docs.rs/cmake/0.1.31/cmake/)  
I have some probleme with the call of cmake.

unlike the example, my source file contains:

.src  
├── fastscan.c  
├── gw\_cdm  
│ └── gw\_cdm.c  
├── gw\_conf  
│ └── gw\_conf.c  
├── gw\_filter  
│ └── gw\_filter.c  
├── gw\_frames\_info\_parser  
│ └── gw\_frames\_info\_parser.c  
├── gw\_modcod  
│ ├── gw\_modcod.c  
│ └── gw\_modcod\_private.h  
├── gw\_net  
│ ├── gw\_net.c  
│ ├── main  
│ └── main.rs

My lib libgw is compuled with cmake and I want just to link gw\_net net with my Rust program

---

<div class="post-metadata">

**Author:** ![souf061](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@souf061](https://users.rust-lang.org/u/souf061)\
**Post date:** [June 21, 2018, 2:29pm UTC](https://users.rust-lang.org/t/linking-library-c-with-rust-programm/18228/5 "2018-06-21T14:29:40Z")

</div>

I'm looking to Use C Code in a Rust project by using Cmake

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [June 21, 2018, 8:44pm UTC](https://users.rust-lang.org/t/linking-library-c-with-rust-programm/18228/6 "2018-06-21T20:44:51Z")

</div>

You can add [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html) and use either cmake or [cc](https://github.com/alexcrichton/cc-rs) crate to compile and link it with the Rust program.

If it's a library that's generally available and you'd like to make it easily usable by anyone, you can wrap that in reusable [sys crate](https://kornel.ski/rust-sys-crate).

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:48am UTC](https://users.rust-lang.org/t/linking-library-c-with-rust-programm/18228/7 "2023-01-12T08:48:25Z")

</div>

Closed on January 12, 2023, 8:48am UTC


